如何强制客户端刷新 JavaScript 文件? [英] How can I force clients to refresh JavaScript files?

查看:33
本文介绍了如何强制客户端刷新 JavaScript 文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们目前正在进行内部测试,因此仍在进行相当快速的更改,但很明显随着使用量开始增加,我们将放慢此过程.话虽如此,我们遇到的一个问题是,在我们使用新的 JavaScript 文件推出更新后,客户端浏览器仍然使用该文件的缓存版本,并且他们看不到更新.显然,在支持呼叫时,我们可以简单地通知他们执行 ctrlF5 刷新以确保他们从服务器获取最新文件,但是最好在那之前处理这个问题.

我们目前的想法是简单地将版本号附加到 JavaScript 文件的名称上,然后在进行更改时,增加脚本上的版本并更新所有引用.这绝对可以完成工作,但更新每个版本的参考可能会很麻烦.

因为我确定我们不是第一个处理这个问题的人,所以我想我会把它扔给社区.当您更新代码时,您如何确保客户端更新其缓存?如果您使用上述方法,您是否使用了简化更改的流程?

解决方案

据我所知,一个常见的解决方案是在脚本的 src 链接中添加一个 ?.

例如:

<script type="text/javascript" src="myfile.js?1500"></script>

<小时><块引用>

此时我认为没有比 find-replace 更好的方法来增加所有脚本标签中的这些版本号"了吗?

您可能有一个版本控制系统为您做这件事?例如,大多数版本控制系统都有一种在签入时自动注入修订号的方法.

它看起来像这样:

<script type="text/javascript" src="myfile.js?$$REVISION$$"></script>

<小时>

当然,总会有更好的解决方案,例如这个.

We are currently working in a private beta and so are still in the process of making fairly rapid changes, although obviously as usage is starting to ramp up, we will be slowing down this process. That being said, one issue we are running into is that after we push out an update with new JavaScript files, the client browsers still use the cached version of the file and they do not see the update. Obviously, on a support call, we can simply inform them to do a ctrlF5 refresh to ensure that they get the up-to-date files from the server, but it would be preferable to handle this before that time.

Our current thought is to simply attach a version number onto the name of the JavaScript files and then when changes are made, increment the version on the script and update all references. This definitely gets the job done, but updating the references on each release could get cumbersome.

As I'm sure we're not the first ones to deal with this, I figured I would throw it out to the community. How are you ensuring clients update their cache when you update your code? If you're using the method described above, are you using a process that simplifies the change?

解决方案

As far as I know a common solution is to add a ?<version> to the script's src link.

For instance:

<script type="text/javascript" src="myfile.js?1500"></script>


I assume at this point that there isn't a better way than find-replace to increment these "version numbers" in all of the script tags?

You might have a version control system do that for you? Most version control systems have a way to automatically inject the revision number on check-in for instance.

It would look something like this:

<script type="text/javascript" src="myfile.js?$$REVISION$$"></script>


Of course, there are always better solutions like this one.

这篇关于如何强制客户端刷新 JavaScript 文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆