最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

html - How to disable caching on a static website? - Stack Overflow

programmeradmin4浏览0评论

I want disable caching on my static website. Currently I'm using the myFile.js?v=123 trick, and update the number every time I make a change. But I'm sure there's a better way of doing this.

This question is not a duplicate of How do we control web page caching, across all browsers? . The accepted answer there was helpful to me, however, it only offers one way of setting the HTTP header that is possible on a static website (I think), and that is using these 3 HTML meta tags:

<meta http-equiv="Cache-Control" content="no-cache, no-store, must-revalidate" />
<meta http-equiv="Pragma" content="no-cache" />
<meta http-equiv="Expires" content="0" />

Unfortunately, the answer also explains that these tags do not take precedence and therefore usually don't work.

Is there any reliable way of disabling cache on a static website, or is what I'm asking for impossible? Thanks for your help!

I want disable caching on my static website. Currently I'm using the myFile.js?v=123 trick, and update the number every time I make a change. But I'm sure there's a better way of doing this.

This question is not a duplicate of How do we control web page caching, across all browsers? . The accepted answer there was helpful to me, however, it only offers one way of setting the HTTP header that is possible on a static website (I think), and that is using these 3 HTML meta tags:

<meta http-equiv="Cache-Control" content="no-cache, no-store, must-revalidate" />
<meta http-equiv="Pragma" content="no-cache" />
<meta http-equiv="Expires" content="0" />

Unfortunately, the answer also explains that these tags do not take precedence and therefore usually don't work.

Is there any reliable way of disabling cache on a static website, or is what I'm asking for impossible? Thanks for your help!

Share Improve this question asked 15 hours ago Petra1999Petra1999 651 silver badge14 bronze badges 1
  • As far as I know, versioning like myFile.js?v=123 is the most effective way to ensure that files are not cached on a static website. – MHD Alaa Alhaj Commented 15 hours ago
Add a comment  | 

1 Answer 1

Reset to default 0

meta tags like Cache-Control aren't guaranteed to work because they don't always take precedence over HTTP headers. Meta tags not often have much effect on caching behavior in some browsers, or they might be overridden by other settings

Best solution is to configure HTTP headers. Make .htaccess file on server and put this code in it

<FilesMatch "\.(html|js|css|jpg|jpeg|png|gif|svg|webp)$">
  Header set Cache-Control "no-store, no-cache, must-revalidate, proxy-revalidate"
</FilesMatch>

Other solution is to use service worker

发布评论

评论列表(0)

  1. 暂无评论