如何用JavaScript清除浏览器缓存?
我们部署了最新的JavaScript代码,但是我们无法获得最新的JavaScript代码。
编者按:这个问题在以下几个地方有半重复,下面第一个问题的答案可能是最好的。这个公认的答案不再是理想的解决方案。
如何强制浏览器重新加载缓存的CSS/JS文件?
如何强制客户端刷新JavaScript文件?
动态重载本地Javascript源/ json数据
如何用JavaScript清除浏览器缓存?
我们部署了最新的JavaScript代码,但是我们无法获得最新的JavaScript代码。
编者按:这个问题在以下几个地方有半重复,下面第一个问题的答案可能是最好的。这个公认的答案不再是理想的解决方案。
如何强制浏览器重新加载缓存的CSS/JS文件?
如何强制客户端刷新JavaScript文件?
动态重载本地Javascript源/ json数据
当前回答
这是我在我的最新项目中使用的一个片段。
来自控制器:
if ( IS_DEV ) {
$this->view->cacheBust = microtime(true);
} else {
$this->view->cacheBust = file_exists($versionFile)
// The version file exists, encode it
? urlencode( file_get_contents($versionFile) )
// Use today's year and week number to still have caching and busting
: date("YW");
}
从视图来看:
<script type="text/javascript" src="/javascript/somefile.js?v=<?= $this->cacheBust; ?>"></script>
<link rel="stylesheet" type="text/css" href="/css/layout.css?v=<?= $this->cacheBust; ?>">
我们的发布过程生成一个包含当前版本版本号的文件。这是通过URL编码文件,并使用它作为缓存破坏者。作为故障转移,如果该文件不存在,则使用年和周数,以便缓存仍能工作,并且至少每周刷新一次。
此外,这为开发环境中的每个页面加载提供了缓存破坏,这样开发人员就不必担心为任何资源(javascript、css、ajax调用等)清除缓存。
其他回答
请不要提供错误的信息。 缓存api是一种不同于http缓存类型的缓存
当服务器发送正确的头文件时,HTTP缓存被触发,你不能用javasvipt访问。
另一方面,缓存api在你想要的时候被触发,它在与service worker一起工作时很有用,所以你可以从这种类型的缓存中交叉请求和回答它 参见:插图1插图2课程
你可以使用这些技巧让你的用户总是有新鲜的内容:
Use location.reload(true) this does not work for me, so I wouldn't recomend it. Use Cache api in order to save into the cache and intersect the request with service worker, be carefull with this one because if the server has sent the cache headers for the files you want to refresh, the browser will answer from the HTTP cache first, and if it does not find it, then it will go to the network, so you could end up with and old file Change the url from you stactics files, my recomendation is you should name it with the change of your files content, I use md5 and then convert it to string and url friendly, and the md5 will change with the content of the file, there you can freely send HTTP cache headers long enough
我建议你看第三部
把这个放在模板的末尾:
var scripts = document.getElementsByTagName('script');
var torefreshs = ['myscript.js', 'myscript2.js'] ; // list of js to be refresh
var key = 1; // change this key every time you want force a refresh
for(var i=0;i<scripts.length;i++){
for(var j=0;j<torefreshs.length;j++){
if(scripts[i].src && (scripts[i].src.indexOf(torefreshs[j]) > -1)){
new_src = scripts[i].src.replace(torefreshs[j],torefreshs[j] + 'k=' + key );
scripts[i].src = new_src; // change src in order to refresh js
}
}
}
如果你使用php可以做:
<script src="js/myscript.js?rev=<?php echo time();?>"
type="text/javascript"></script>
由于浏览器缓存相同的链接,您应该在url末尾添加一个随机数。 new Date(). gettime()生成一个不同的数字。
只需添加新的Date().getTime()链接结束 调用
'https://stackoverflow.com/questions.php?' + new Date().getTime()
输出:https://stackoverflow.com/questions.php?1571737901173
在PHP中,你也可以强制每小时重新加载一次代码,就像这样:
<?php
echo '<script language="JavaScript" src="js/myscript.js?token='.date('YmdH').'">';
?>
or
<script type="text/javascript" src="js/myscript.js?v=<?php echo date('YmdHis'); ?>"></script>