How to force the browser to reload cached CSS/JS files?

后端 未结 30 4359
青春惊慌失措
青春惊慌失措 2020-11-21 05:49

I have noticed that some browsers (in particular, Firefox and Opera) are very zealous in using cached copies of .css and .js files, even be

30条回答
  •  再見小時候
    2020-11-21 05:50

    Instead of changing the version manually, I would recommend you use an MD5 hash of the actual CSS file.

    So your URL would be something like

    http://mysite.com/css/[md5_hash_here]/style.css
    

    You could still use the rewrite rule to strip out the hash, but the advantage is that now you can set your cache policy to "cache forever", since if the URL is the same, that means that the file is unchanged.

    You can then write a simple shell script that would compute the hash of the file and update your tag (you'd probably want to move it to a separate file for inclusion).

    Simply run that script every time CSS changes and you're good. The browser will ONLY reload your files when they are altered. If you make an edit and then undo it, there's no pain in figuring out which version you need to return to in order for your visitors not to re-download.

提交回复
热议问题