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.

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

我确信我们不是第一个处理这个问题的人,我想我应该把它扔给社区。当你更新你的代码时,你如何确保客户端更新他们的缓存?如果您正在使用上面描述的方法,那么您使用的是简化更改的过程吗?


当前回答

使用file.js的好处是什么?V=1 / fileV1.js的优点是不需要在服务器上存储多个版本的JavaScript文件。

我看到file.js的问题?V=1是在使用新版本的库实用程序时,另一个JavaScript文件中的依赖代码可能会中断。

为了向后兼容,我认为最好对新页面使用jQuery.1.3.js,而让现有页面使用jQuery.1.1.js,直到你准备好升级旧页面为止(如果有必要的话)。

其他回答

我们一直在为用户创建一个SaaS,并为他们提供一个脚本,以附加在他们的网站页面,它是不可能附加一个版本的脚本,因为用户将附加脚本到他们的网站的功能,我不能强迫他们改变版本,每次我们更新脚本

因此,我们找到了一种方法,在用户每次调用原始脚本时加载脚本的新版本

提供给用户的脚本链接

<script src="https://thesaasdomain.com/somejsfile.js" data-ut="user_token"></script>

脚本文件

if($('script[src^="https://thesaasdomain.com/somejsfile.js?"]').length !== 0) {
   init();
} else {
   loadScript("https://thesaasdomain.com/somejsfile.js?" + guid());
}

var loadscript = function(scriptURL) {
   var head = document.getElementsByTagName('head')[0];
   var script = document.createElement('script');
   script.type = 'text/javascript';
   script.src = scriptURL;
   head.appendChild(script);
}

var guid = function() {
    return 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, function(c) {
        var r = Math.random() * 16 | 0, v = c == 'x' ? r : (r & 0x3 | 0x8);
        return v.toString(16);
    });
}

var init = function() {
    // our main code
}

解释:

用户已经在他们的网站上附加了脚本,我们使用jQuery选择器检查脚本附加的唯一标记是否存在,如果不存在,则使用更新的标记(或版本)动态加载它

这是调用相同的脚本两次,这可能是一个性能问题,但它确实解决了强制脚本不从缓存加载的问题,而不将版本放在给定给用户或客户端的实际脚本链接中

免责声明:如果在您的情况下性能是一个大问题,请不要使用。

一个简单的方法。 编辑htaccess

RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_URI} \.(jpe?g|bmp|png|gif|css|js|mp3|ogg)$ [NC]
RewriteCond %{QUERY_STRING} !^(.+?&v33|)v=33[^&]*(?:&(.*)|)$ [NC]
RewriteRule ^ %{REQUEST_URI}?v=33 [R=301,L]

你可以添加文件版本到你的文件名,这样它就像:

https://www.example.com/script_fv25.js

fv25 =>文件版本号25

在你的.htaccess中放入这个块,它将从链接中删除版本部分:

RewriteEngine On
RewriteRule (.*)_fv\d+\.(js|css|txt|jpe?g|png|svg|ico|gif) $1.$2 [L]

所以最后一个链接是:

https://www.example.com/script.js

虽然它是特定于框架的,但Django 1.4拥有staticfiles应用程序功能,其工作方式与上面答案中的“greenfelt”站点类似

ASP。NET页面我正在使用以下

之前

<script src="/Scripts/pages/common.js" type="text/javascript"></script>

AFTER(强制重载)

<script src="/Scripts/pages/common.js?ver<%=DateTime.Now.Ticks.ToString()%>" type="text/javascript"></script>

添加DateTime.Now.Ticks效果很好。