对 Javascript 文件进行版本控制以防止不必要的缓存清除

发布于 2024-12-04 06:05:34 字数 293 浏览 0 评论 0原文

我对所有客户端 JS 文件进行版本控制,例如“/js/myfile.js?v=3903948”,这样我的客户就不需要清除浏览器缓存来确保获得更新的文件。但每次我推送更新时,至少会有一个人遇到运行旧版本并出现某种错误的问题。我曾经以为这只是他们在发布期间已经出现在页面上并且只需要重新加载浏览器,但今天这种情况发生在我身上,当时我之前肯定不在页面上。我浏览到实时站点并运行旧代码。我需要在该页面上刷新浏览器才能获取新文件。

什么会导致这种情况?

PS 我在 Win7 上使用 Chrome,但我之前见过客户在所有不同的浏览器上报告过此问题。

I version all of my client side JS files like "/js/myfile.js?v=3903948" so that my clients don't need to clear their browser cache to ensure they get the updated files. But every time I push an update, without fail, at least one person runs into a problem where they are running the old version and get some kind of error. I used to think that this was just them having already been on the page during the release and just needing to reload the browser, but this happened to me today when I was definitely not previously on the page. I browsed to the live site and was running the old code. I needed to do a browser refresh on that page to get the new file.

What can cause this?

PS I was using Chrome on Win7, but I have seen clients report this before on all different browsers.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

茶花眉 2024-12-11 06:05:34

如果你的主网页也可以被缓存,那么该页面的旧版本可以请求旧版本的JS文件。如果实际引用 JS 文件的页面无法缓存或缓存时间非常短,则 JS 文件版本控制效果最佳。

If your main web page can also be cached, then the old version of that page can be requesting the old version of the JS file. JS file versioning works best if the page that actually refers to the JS file cannot be cached or has very short caching time.

吲‖鸣 2024-12-11 06:05:34

我同意jfriend00关于网页本身被兑现并因此请求旧的javascript版本的观点。

为了防止这种情况,您可以通过 ajax(Post)请求加载 javascript 文件,要么请求服务器下载要下载的准确(最新)版本号,要么请求 javascript 本身并将其插入,例如在页。

编辑:例如参见此处

I agree with jfriend00 about the webpage itself being cashed and thus requesting the old javascript version.

To prevent this, you can have the javascript file loaded by an ajax (Post) request, either requesting the server what is the accurate(latest) version number to download, or requesting the javascript itself and inserting it, e.g. in the head of the page.

Edit: see for example here

心碎的声音 2024-12-11 06:05:34

我向服务器发出一个快速 AJAX 请求,获取服务器期望的版本,然后在客户端脚本旧时强制服务器刷新页面。

I make a quick AJAX request to the server for the version it expects them to have, then force them to refresh the page if the client's script is old.

巷雨优美回忆 2024-12-11 06:05:34

似乎代理或某些负载均衡器正在提供旧内容而不是新内容。还要检查 IIS/网络服务器设置这些文件如何缓存/过期。

您可以使用 Fiddler 等工具检查线路上发生的情况。

Seems that proxy or some load balancer is serving old content instead of new. Also check IIS/webserver settings how are these files cached/expired.

You can check what is going on on the wire with tools like Fiddler.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文