简体   繁体   English

如何使静态文件上的HTTP缓存头无效

[英]How to invalidate HTTP Cache Headers on static files

I have set a CacheControl declaration in my .htaccess for browsers to cache static files. 我在.htaccess中设置了CacheControl声明,以供浏览器缓存静态文件。

Upon successive deployments, how can I define an instruction to invalidate the client's cache? 在连续部署后,如何定义指令以使客户端的缓存无效?

<Files *.js>
Header add "Cache-Control" "max-age=604800"
</Files>

how can I define an instruction to invalidate the client's cache? 如何定义一条使客户端缓存无效的指令?

The short answer is that you can't - HTTP protocol doesn't support it. 简短的答案是您不能-HTTP协议不支持它。

But there is a sneaky way to do it: create a manifest html page listing all the assets which are cacheable, load it in an iframe then use Javascript to do a refresh on the iframe. 但是有一种偷偷摸摸的方法:创建一个清单html页面,列出所有可缓存的资产,将其加载到iframe中,然后使用Javascript对iframe进行刷新。 This will create conditional requests for the content to the server - and if your server responds with a 2xx status the new content supplied will overwrite the cache. 这将向服务器创建对内容的条件请求-如果服务器以2xx状态响应,则提供的新内容将覆盖缓存。

However just because it's possible DOES NOT MAKE IT A GOOD IDEA (for lots of reasons - don't ask unless you want to pay me to come and provide consultancy and training for a couple of weeks); 但是,只是因为有可能不使它成为一个好主意(出于很多原因-除非您想付钱给我来提供咨询和培训几个星期,否则请不要问); Do not try this at home! 不要在家尝试做这个!

The general consensus is that the best way to manage caching is to serve up content with a very long TTL but to embed versioning information in the URL . 普遍的共识是,管理缓存的最佳方法是提供具有长TTL的内容,但将版本信息嵌入URL中

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM