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文件的名称上,然后当进行更改时,增加脚本上的版本并更新所有引用。这肯定可以完成工作,但是在每个版本上更新引用可能会很麻烦。

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


当前回答

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

例如:

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

我假设在这一点上,没有更好的方法比查找-替换增量这些“版本号”在所有的脚本标签?

你可以让版本控制系统帮你做这件事?例如,大多数版本控制系统都有一种方法可以在签入时自动注入修订号。

它看起来是这样的:

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

当然,总有更好的解决方案。

其他回答

现在常见的做法是生成一个内容哈希代码作为文件名的一部分,以迫使浏览器(尤其是IE)重新加载javascript文件或css文件。

例如,

vendor.a7561fb0e9a071baadb9.js main.b746e3eb72875af2caa9.js

这通常是构建工具(如webpack)的工作。这里有更多的细节,如果有人想尝试如果你正在使用webpack。

前端的选择

我专门为那些不能更改后端任何设置的人编写了此代码。在这种情况下,防止超长缓存的最佳方法是:

new Date().getTime()

然而,对于大多数程序员来说,缓存可能需要几分钟或几个小时,所以上面的简单代码最终会迫使所有用户下载“所浏览的每个页面”。为了指定这个项目在不重新加载的情况下将保持多长时间,我编写了以下代码,并在下面留下了几个示例:

// cache-expires-after.js v1
function cacheExpiresAfter(delay = 1, prefix = '', suffix = '') { // seconds
    let now = new Date().getTime().toString();
    now = now.substring(now.length - 11, 10); // remove decades and milliseconds
    now = parseInt(now / delay).toString();
    return prefix + now + suffix;
};

// examples (of the delay argument):
// the value changes every 1 second
var cache = cacheExpiresAfter(1);
// see the sync
setInterval(function(){
    console.log(cacheExpiresAfter(1), new Date().getSeconds() + 's');
}, 1000);

// the value changes every 1 minute
var cache = cacheExpiresAfter(60);
// see the sync
setInterval(function(){
    console.log(cacheExpiresAfter(60), new Date().getMinutes() + 'm:' + new Date().getSeconds() + 's');
}, 1000);

// the value changes every 5 minutes
var cache = cacheExpiresAfter(60 * 5); // OR 300

// the value changes every 1 hour
var cache = cacheExpiresAfter(60 * 60); // OR 3600

// the value changes every 3 hours
var cache = cacheExpiresAfter(60 * 60 * 3); // OR 10800

// the value changes every 1 day
var cache = cacheExpiresAfter(60 * 60 * 24); // OR 86400

// usage example:
let head = document.head || document.getElementsByTagName('head')[0];
let script = document.createElement('script');
script.setAttribute('src', '//unpkg.com/sweetalert@2.1.2/dist/sweetalert.min.js' + cacheExpiresAfter(60 * 5, '?'));
head.append(script);

// this works?
let waitSwal = setInterval(function() {
    if (window.swal) {
        clearInterval(waitSwal);
        swal('Script successfully injected', script.outerHTML);
    };
}, 100);

使用版本GET变量来防止浏览器缓存。

在url末尾附加?v=AUTO_INCREMENT_VERSION可以防止浏览器缓存-避免任何和所有缓存的脚本。

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

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

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

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

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