有没有一种方法可以让我把一些代码放在我的页面上,这样当有人访问一个网站时,它就会清空浏览器缓存,这样他们就可以查看更改了?
使用语言:ASP。净,VB。NET,当然还有HTML, CSS和jQuery。
有没有一种方法可以让我把一些代码放在我的页面上,这样当有人访问一个网站时,它就会清空浏览器缓存,这样他们就可以查看更改了?
使用语言:ASP。净,VB。NET,当然还有HTML, CSS和jQuery。
当前回答
很多答案都忽略了一点——大多数开发人员都很清楚关闭缓存是低效的。然而,在许多常见的情况下,效率并不重要,默认缓存行为被严重破坏。
这些包括嵌套的、迭代的脚本测试(大的一个!)和坏的第三方软件变通方法。这里给出的解决方案都不足以解决这种常见场景。大多数web浏览器都有过于激进的缓存,并且没有提供合理的方法来避免这些问题。
其他回答
我实现了这个简单的解决方案(还没有在生产环境中):
function verificarNovaVersio() {
var sVersio = localStorage['gcf_versio'+ location.pathname] || 'v00.0.0000';
$.ajax({
url: "./versio.txt"
, dataType: 'text'
, cache: false
, contentType: false
, processData: false
, type: 'post'
}).done(function(sVersioFitxer) {
console.log('Versió App: '+ sVersioFitxer +', Versió Caché: '+ sVersio);
if (sVersio < (sVersioFitxer || 'v00.0.0000')) {
localStorage['gcf_versio'+ location.pathname] = sVersioFitxer;
location.reload(true);
}
});
}
我有一个小文件位于html的位置:
“versio.txt”:
v00.5.0014
这个函数在我的所有页面中都被调用,因此在加载时它会检查localStorage的版本值是否低于当前版本,并执行a
location.reload(true);
...强制从服务器而不是缓存重新加载。
(显然,您可以使用cookie或其他持久客户端存储而不是localStorage)
我选择这个解决方案是因为它很简单,因为只维护一个“version .txt”文件就会强制重载整个站点。
queryString方法很难实现,而且也是缓存的(如果您从v1.1更改到以前的版本将从缓存加载,那么这意味着缓存不会刷新,将所有以前的版本保存在缓存中)。
我是一个小新手,我很感激你的专业检查和审查,以确保我的方法是一个很好的方法。
希望能有所帮助。
您是想清除缓存,还是只是确保当前(已更改的?)页面没有缓存?
如果是后者,那就应该如此简单
<META HTTP-EQUIV="Pragma" CONTENT="no-cache">
强制浏览器清除缓存或重新加载正确的数据?我已经尝试了stackoverflow中描述的大多数解决方案,一些工作,但过了一会儿,它最终缓存并显示先前加载的脚本或文件。是否有另一种方法,将清除缓存(css, js等),并实际工作在所有浏览器?
I found so far that specific resources can be reloaded individually if you change the date and time on your files on the server. "Clearing cache" is not as easy as it should be. Instead of clearing cache on my browsers, I realized that "touching" the server files cached will actually change the date and time 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/sample/file1.css');
touch('/www/sample/file2.js');
?>
然后……剩下的程序…
It took me some time to resolve this issue (as many browsers act differently to different commands, but they all check time of files and compare to your downloaded copy in your browser, if different date and time, will do the refresh), If you can't go the supposed right way, there is always another usable and better solution to it. Best Regards and happy camping. By the way touch(); or alternatives work in many programming languages inclusive in javascript bash sh php and you can include or call them in html.
这是我在一个使用PHP的应用程序中使用的简单解决方案。
所有JS和CSS文件都放在一个有版本名的文件夹中。示例:"1.0.01"
root\1.0.01\JS
root\1.0.01\CSS
在那里创建一个Helper并定义版本号
<?php
function system_version()
{
return '1.0.07';
}
和链接JS和SCC文件如下
<script src="<?= base_url(); ?>/<?= system_version();?>/js/generators.js" type="text/javascript"></script>
<link rel="stylesheet" type="text/css" href="<?= base_url(); ?>/<?= system_version(); ?>/css/view-checklist.css" />
每当我对任何JS或CSS文件进行更改时,我都会在Helper中更改System Verson并重命名文件夹并部署它。
对于webpack用户:-
我在webpack配置中添加了chunkash。这解决了每次部署时缓存失效的问题。我们还需要注意index.html/ asset。清单不是缓存在你的CDN或浏览器。在webpack配置中的块名配置将看起来像这样:-
文件名:[chunkhash] - $ {Date.now ()} . js
或者如果你使用contenthash那么
文件名:[contenthash] - $ {Date.now ()} . js