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 mvc中,你可以使用@DateTime.UtcNow.ToString()来表示js文件的版本号。版本号自动改变日期和你强迫客户端浏览器自动刷新js文件。我使用这种方法,这是工作良好。

<script src="~/JsFilePath/JsFile.js?v=@DateTime.UtcNow.ToString()"></script>

其他回答

在PHP中:

function latest_version($file_name){
    echo $file_name."?".filemtime($_SERVER['DOCUMENT_ROOT'] .$file_name);
}

在HTML中:

<script type="text/javascript" src="<?php latest_version('/a-o/javascript/almanacka.js'); ?>">< /script>

工作原理:

在HTML中,像往常一样写文件路径和名称,但只在函数中。 PHP获取文件的文件时间,并返回文件路径+名称+”?+最新更改时间

你可以用.htaccess来实现

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

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

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

例如:

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

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

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

它看起来是这样的:

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

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

在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有一个很好的例子和解释缓存破坏这里(页面底部)

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

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