Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
285 views
in Technique[技术] by (71.8m points)

javascript - 如何强制客户端刷新JavaScript文件?(How can I force clients to refresh JavaScript files?)

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.

(话虽这么说,我们遇到的一个问题是,在推出新JavaScript文件的更新之后,客户端浏览器仍然使用文件的缓存版本,而他们看不到更新。)

Obviously, on a support call, we can simply inform them to do a ctrl F5 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.

(显然,在技术支持电话上,我们可以简单地通知他们进行ctrl F5刷新,以确保他们从服务器获取最新文件,但是最好在此之前进行处理。)

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.

(我们当前的想法是简单地将版本号附加到JavaScript文件的名称上,然后在进行更改时,增加脚本上的版本并更新所有引用。)

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?

(如果您使用上述方法,是否正在使用简化更改的过程?)

  ask by AdamB translate from so

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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

(据我所知,一个常见的解决方案是在脚本的src链接中添加?<version> 。)

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 .

(当然,也有像往常一样,更好的解决方案这一个 。)


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...