如何用JavaScript清除浏览器缓存?
我们部署了最新的JavaScript代码,但是我们无法获得最新的JavaScript代码。
编者按:这个问题在以下几个地方有半重复,下面第一个问题的答案可能是最好的。这个公认的答案不再是理想的解决方案。
如何强制浏览器重新加载缓存的CSS/JS文件?
如何强制客户端刷新JavaScript文件?
动态重载本地Javascript源/ json数据
如何用JavaScript清除浏览器缓存?
我们部署了最新的JavaScript代码,但是我们无法获得最新的JavaScript代码。
编者按:这个问题在以下几个地方有半重复,下面第一个问题的答案可能是最好的。这个公认的答案不再是理想的解决方案。
如何强制浏览器重新加载缓存的CSS/JS文件?
如何强制客户端刷新JavaScript文件?
动态重载本地Javascript源/ json数据
当前回答
你也可以用元HTML标签禁用浏览器缓存,只要把HTML标签放在头部部分,以避免网页被缓存,而你正在编码/测试,当你完成时,你可以删除元标签。
(在标题部分)
<meta http-equiv="Cache-Control" content="no-cache, no-store, must-revalidate" />
<meta http-equiv="Pragma" content="no-cache" />
<meta http-equiv="Expires" content="0"/>
在头部粘贴后刷新页面,应该也会刷新新的javascript代码。
如果你需要的话,这个链接会给你其他选择 http://cristian.sulea.net/blog/disable-browser-caching-with-meta-html-tags/
或者你可以像这样创建一个按钮
<button type="button" onclick="location.reload(true)">Refresh</button>
它会刷新并避免缓存,但它会在你的页面上,直到你完成测试,然后你可以把它拿掉。第一个选择是最好的选择。
其他回答
大多数正确答案在这个话题中已经提到了。然而,我想添加一篇文章的链接,这是我能读到的最好的一篇文章。
https://www.fastly.com/blog/clearing-cache-browser
在我看来,最合适的解决方案是:
POST在一个iframe中。下面是建议帖子的一个小删减:
=============
const ifr = document.createElement('iframe');
ifr.name = ifr.id = 'ifr_'+Date.now();
document.body.appendChild(ifr);
const form = document.createElement('form');
form.method = "POST";
form.target = ifr.name;
form.action = ‘/thing/stuck/in/cache’;
document.body.appendChild(form);
form.submit();
有一些明显的副作用:这将创建一个浏览器历史记录条目,并受到响应未缓存的相同问题的影响。但它逃脱了fetch存在的预飞行要求,并且由于它是一个导航,分割缓存的浏览器将清除正确的缓存。
这个几乎能搞定。Firefox将为跨源资源保留卡住的对象,但仅用于后续获取。每个浏览器都将使对象的导航缓存无效,无论是同一源资源还是跨源资源。
==============================
我们尝试了很多方法,但是这个很有效。唯一的问题是,你需要能够以某种方式将这个脚本带到最终用户页面,这样你就能够重置缓存。在这种特殊情况下,我们是幸运的。
除了每小时或每周缓存一次之外,您还可以根据文件数据进行缓存。
示例(PHP):
<script src="js/my_script.js?v=<?=md5_file('js/my_script.js')?>"></script>
甚至使用文件修改时间:
<script src="js/my_script.js?v=<?=filemtime('js/my_script.js')?>"></script>
Maybe "clearing cache" is not as easy as it should be. Instead of clearing cache on my browsers, I realized that "touching" the file will actually change the date of the source file cached on the server (Tested on Edge, Chrome and Firefox) and most browsers will automatically download the most current fresh copy of whats on your server (code, graphics any multimedia too). I suggest you just copy the most current scripts on the server and "do the touch thing" solution before your program runs, so it will change the date of all your problem files to a most current date and time, then it downloads a fresh copy to your browser:
<?php
touch('/www/control/file1.js');
touch('/www/control/file2.js');
touch('/www/control/file2.js');
?>
...剩下的程序…
我花了一些时间来解决这个问题(因为许多浏览器对不同的命令有不同的行为,但它们都检查文件的时间,并将其与浏览器中下载的副本进行比较,如果日期和时间不同,将进行刷新),如果你不能按照假设的正确方式进行,总有另一种可用的更好的解决方案。祝你露营愉快。
把这个放在模板的末尾:
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
}
}
}
或者你可以通过服务器用file_get_contts读取js文件,然后在js内容的头中放入echo