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

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


当前回答

jQuery函数getScript也可以用来确保每次页面加载时确实加载了js文件。

我是这样做的:

$(document).ready(function(){
    $.getScript("../data/playlist.js", function(data, textStatus, jqxhr){
         startProgram();
    });
});

请登录http://api.jquery.com/jQuery.getScript/查看该功能

默认情况下,$. getscript()将缓存设置设置为false。这将向请求URL附加一个带有时间戳的查询参数,以确保浏览器在每次请求脚本时都会下载脚本。

其他回答

一个简单的技巧,我可以很好地防止新旧javascript文件之间的冲突。这意味着:如果出现冲突和错误,将提示用户按Ctrl-F5。

在页面顶部添加如下内容

<h1 id="welcome"> Welcome to this page <span style="color:red">... press Ctrl-F5</span></h1>

看起来像

让这一行javascript作为加载页面时最后执行的代码:

document.getElementById("welcome").innerHTML = "Welcome to this page"

如果没有发生错误,上面的欢迎问候语几乎不可见,几乎立即被替换为

在ASP缓存破坏。NET Core通过标记助手将为您处理这个问题,并允许您的浏览器保留缓存的脚本/css,直到文件更改。只需在你的script (js)或link (css)标签中添加标签助手asp-追加-version="true":

<link rel="stylesheet" href="~/css/site.min.css" asp-append-version="true"/>

Dave Paquette有一个很好的例子和解释缓存破坏这里(页面底部)

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

例如,

vendor.a7561fb0e9a071baadb9.js main.b746e3eb72875af2caa9.js

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

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

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

前端的选择

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

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);