How to ensure broswer does not cache my JS/CSS [du

2019-08-13 20:12发布

Possible Duplicate:
Css file caching

I want to ensure users always get the latest version of my code.

However setting it up so that browsers do not cache the files is bad as it would need to be loaded fresh every page view. I want to ensure it doesn't use a cached copy from a previous release. Eg. using a cached copy of the release 1.2 JS with version 1.3 site.

One way I have seen involves changing the filename on each release and linking to that different file name on each release (eg 'myscript1.3.js') This seems a lot of effort and slightly hacky.

Is there a better way to do this?

2条回答
欢心
2楼-- · 2019-08-13 20:28

That's a perfectly good solution, and I would even go so far as to say that it's the best.

You could also use query parameters to indicate a new version (?v=1, ?v=2, ...). But this could throw off some public proxies (they might not cache the files). See here for details: https://developers.google.com/speed/docs/best-practices/caching#LeverageProxyCaching

Off course this will require some effort on your part. But you can automate all of this in your deploy process (how to do that would depend entirely on your setup, so I can't give you a solution without knowing a lot more about your project).

查看更多
Ridiculous、
3楼-- · 2019-08-13 20:34

Method 1 : You alter server headers.These might be different for different servers but for apache servers you can have a look at the mod_cache module http://httpd.apache.org/docs/2.4/mod/mod_cache.html

Method 2 : You use some meta tags.You'll find a related question here Using <meta> tags to turn off caching in all browsers?

查看更多
登录 后发表回答