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附加一个带有时间戳的查询参数,以确保浏览器在每次请求脚本时都会下载脚本。

其他回答

你可以用.htaccess来实现

在你的.htaccess文件中添加以下代码行:

# DISABLE CACHING
<IfModule mod_headers.c>
  <FilesMatch "\.js$">
      Header set Cache-Control "no-store, max-age=0"
  </FilesMatch>
</IfModule>

谷歌Page-Speed:不要在静态资源的URL中包含查询字符串。 大多数代理,尤其是Squid到3.0版本,不会缓存URL中带有"?"的资源,即使响应中存在cache -control: public报头。要为这些资源启用代理缓存,请从静态资源引用中删除查询字符串,并将参数编码到文件名本身中。

在这种情况下,您可以将版本包含到URL ex: http://abc.com/v1.2/script.js中,并使用apache mod_rewrite将链接重定向到http://abc.com/script.js。当您更改版本时,客户端浏览器将更新新文件。

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

我是这样做的:

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

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

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

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

例如,

vendor.a7561fb0e9a071baadb9.js main.b746e3eb72875af2caa9.js

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

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效果很好。