浏览器缓存 javascript 和 css 文件
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6311694/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Browser caching javascript and css files
提问by Dusty
I understand that the browser is forced to fetch a new version of the cached JS file when the file name is changed or a query string is added to it.
我知道当文件名更改或向其中添加查询字符串时,浏览器会被迫获取缓存 JS 文件的新版本。
We don't do this and until now we've never had issues with browser serving stale files. Recently, we are seeing some users using IE9 who complain about the browser serving cached JS/CSS files. This issue is not consistent across everyone using the site.
我们不这样做,直到现在我们从未遇到过浏览器提供陈旧文件的问题。最近,我们看到一些使用 IE9 的用户抱怨浏览器提供缓存的 JS/CSS 文件。这个问题在使用该网站的每个人中并不一致。
My understanding is that when the file name or query string is not changed, but the JS file content is changed, the browser would fetch the new version.
我的理解是,当文件名或查询字符串没有改变,但JS文件内容改变时,浏览器会获取新版本。
Why is this happening and why is it not consistent?
为什么会发生这种情况,为什么不一致?
Any thoughts?
有什么想法吗?
回答by mch_dk
Setting an expiry date or a maximum age in the HTTP headers for static resources instructs the browser to load previously downloaded resources from local disk rather than over the network.
在静态资源的 HTTP 标头中设置到期日期或最长期限会指示浏览器从本地磁盘而不是通过网络加载先前下载的资源。
This is good if we want to actual cache the resource. If we want to force a new download set no-cache, which forces caches to submit the request to the origin server for validation before releasing a cached copy, every time. This is useful to assure that authentication is respected (in combination with public), or to maintain rigid freshness, without sacrificing all of the benefits of caching.
如果我们想实际缓存资源,这很好。如果我们想强制一个新的下载集 no-cache,这会强制缓存每次在释放缓存副本之前将请求提交给源服务器进行验证。这对于确保尊重身份验证(与 public 结合)或保持严格的新鲜度非常有用,而不会牺牲缓存的所有好处。
回答by Sparky
Yes, when the content seems the same (i.e. same file names), users may get a cached version of those files on subsequent visits.
是的,当内容看起来相同(即相同的文件名)时,用户可能会在后续访问时获得这些文件的缓存版本。
It is really beyond your control... it's up to each specific browser to decide how to handle caching and it's also up to the user... some dump their cache regularly or refresh the page if something doesn't seem right.
这真的超出了你的控制……这取决于每个特定的浏览器来决定如何处理缓存,这也取决于用户……有些人会定期转储他们的缓存,或者如果出现问题,则刷新页面。
If you want to force the user to see your updated CSS or JS content, change the CSS or JS file name... otherwise it may be inconsistent for a short, but unknown, period of time.
如果您想强制用户看到您更新的 CSS 或 JS 内容,请更改 CSS 或 JS 文件名...否则可能会在短暂但未知的一段时间内不一致。
This tutorial may help you...
本教程可以帮助您...
回答by SergeS
For example Chrome caches scripts until Shift + F5 or some time expired ( and ignore the fact it is changed on server, it don't even send a request ).
例如,Chrome 缓存脚本直到 Shift + F5 或一段时间到期(并忽略它在服务器上更改的事实,它甚至不发送请求)。
So is done by other browsers ( but when cache is enabled ) - i cannot descripe exactly when it happens
其他浏览器也是如此(但是当启用缓存时) - 我无法准确描述它何时发生