How can I ensure that the latest version of my jav

2019-06-25 10:15发布

We have a client with thousands of users (who all use Internet Explorer) and a large amount of javascript files that enhance their user experience with our product.

The problem I'm having is that any time we update one of these scripts there is no way to know whether the client is seeing the latest version. What we're having to do is tell our client to do a hard refresh (ctrl+f5) before viewing any changes. Obviously this approach is not ideal.

I know that browsers cache based on the url, so one could use something like

<script src='myScript.js?ver=1.2'>

to get around the issue, but this is not an option for us.

I was hoping that there's some kind of header property or something similar that we could use to tell IE not to cache these scripts.

Any ideas?

2条回答
我想做一个坏孩纸
2楼-- · 2019-06-25 10:18

You can also version the filename itself like jQuery does:

<script src='myScript-v1-2.js'>

Then, each time you revise the script, you bump the version number and modify the pages that include it to point to the name of the new script. This is foolproof vs. caching, yet still allows your viewers to receive the maximum benefit of caching and requires no server configuration changes for the .js file.

A full solution will typically include setting a relatively short cache lifetime for your host web page and then allow the various resources (stylesheet files, JS files, images, etc...) to have longer cache lifetimes for maximum caching. Anything that is fingerprinted can have a very long cache lifetime. See the reference that fabianhjr posted about for ways to set the cache lifetime of the host web page. It can be done in the web page itself (<meta> settings) or in the http headers via the server.

If you turn off caching for your script file (which would likely have to be done at the web server level for a script file) then all your viewers will lose the performance benefit of caching and you will lose the bandwidth and load-saving benefit of caching. If you use a common .JS file across many pages (a common design pattern), your viewers will see slower performance on every page.

查看更多
虎瘦雄心在
3楼-- · 2019-06-25 10:24

Everything you need to know about cache http://www.mnot.net/cache_docs/

http://www.mnot.net/cache_docs/#CACHE-CONTROL <-- HTTP Headers

查看更多
登录 后发表回答