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

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


当前回答

ASP。NET我认为下一个具有高级选项的解决方案(调试/发布模式,版本):

Js或Css文件包括如下方式:

<script type="text/javascript" src="Scripts/exampleScript<%=Global.JsPostfix%>" />
<link rel="stylesheet" type="text/css" href="Css/exampleCss<%=Global.CssPostfix%>" />

全球。JsPostfix和Global。在Global.asax中,CssPostfix的计算方法如下:

protected void Application_Start(object sender, EventArgs e)
{
    ...
    string jsVersion = ConfigurationManager.AppSettings["JsVersion"];
    bool updateEveryAppStart = Convert.ToBoolean(ConfigurationManager.AppSettings["UpdateJsEveryAppStart"]);
    int buildNumber = System.Reflection.Assembly.GetExecutingAssembly().GetName().Version.Revision;
    JsPostfix = "";
#if !DEBUG
    JsPostfix += ".min";
#endif      
    JsPostfix += ".js?" + jsVersion + "_" + buildNumber;
    if (updateEveryAppStart)
    {
        Random rand = new Random();
        JsPosfix += "_" + rand.Next();
    }
    ...
}

其他回答

location.reload(真正的);

参见https://www.w3schools.com/jsref/met_loc_reload.asp

我动态调用这行代码,以确保javascript已经从web服务器重新检索,而不是从浏览器的缓存,以避免这个问题。

最简单的解决方案?不要让浏览器缓存。将当前时间(以毫秒为单位)作为查询追加。

(您仍然处于测试阶段,因此您可以合理地选择不优化性能。但这里有YMMV。)

这种用法已被弃用: https://developer.mozilla.org/en-US/docs/Web/HTML/Using_the_application_cache

这个答案只晚了6年,但我在很多地方都没有看到这个答案……HTML5引入了应用程序缓存来解决这个问题。我发现我正在编写的新服务器代码正在破坏人们浏览器中存储的旧javascript,所以我想找到一种方法让他们的javascript过期。使用如下所示的清单文件:

CACHE MANIFEST
# Aug 14, 2014
/mycode.js

NETWORK:
*

并在每次您希望用户更新其缓存时,使用新的时间戳生成此文件。作为旁注,如果您添加了这个,浏览器将不会重新加载(即使当用户刷新页面时),直到清单告诉它。

并非所有浏览器都使用'?’在里面。我所做的是确保它被尽可能多地缓存,我将版本包含在文件名中。

而不是stuff。js?我用stuff_123.js

我使用mod_redirect(我认为)在apache有stuff_*.js去stuff.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