有没有一种方法可以让我把一些代码放在我的页面上,这样当有人访问一个网站时,它就会清空浏览器缓存,这样他们就可以查看更改了?
使用语言:ASP。净,VB。NET,当然还有HTML, CSS和jQuery。
有没有一种方法可以让我把一些代码放在我的页面上,这样当有人访问一个网站时,它就会清空浏览器缓存,这样他们就可以查看更改了?
使用语言:ASP。净,VB。NET,当然还有HTML, CSS和jQuery。
当前回答
我有一个案例,我将在线拍摄客户端的照片,如果照片被更改,就需要更新div。浏览器仍然显示着旧照片。因此,我使用了调用随机GET变量的方法,该变量每次都是唯一的。这就是它,如果它能帮助到任何人的话
<img src="/photos/userid_73.jpg?random=<?php echo rand() ?>" ...
编辑 正如其他人指出的那样,下面是更有效的解决方案,因为它只会在图像被更改时重新加载图像,通过文件大小识别这种更改:
<img src="/photos/userid_73.jpg?modified=<? filemtime("/photos/userid_73.jpg")?>"
其他回答
我实现了这个简单的解决方案(还没有在生产环境中):
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更改到以前的版本将从缓存加载,那么这意味着缓存不会刷新,将所有以前的版本保存在缓存中)。
我是一个小新手,我很感激你的专业检查和审查,以确保我的方法是一个很好的方法。
希望能有所帮助。
对于webpack用户:-
我在webpack配置中添加了chunkash。这解决了每次部署时缓存失效的问题。我们还需要注意index.html/ asset。清单不是缓存在你的CDN或浏览器。在webpack配置中的块名配置将看起来像这样:-
文件名:[chunkhash] - $ {Date.now ()} . js
或者如果你使用contenthash那么
文件名:[contenthash] - $ {Date.now ()} . js
我也遇到过类似的问题,我是这样解决的:
In index.html file I've added manifest: <html manifest="cache.manifest"> In <head> section included script updating the cache: <script type="text/javascript" src="update_cache.js"></script> In <body> section I've inserted onload function: <body onload="checkForUpdate()"> In cache.manifest I've put all files I want to cache. It is important now that it works in my case (Apache) just by updating each time the "version" comment. It is also an option to name files with "?ver=001" or something at the end of name but it's not needed. Changing just # version 1.01 triggers cache update event. CACHE MANIFEST # version 1.01 style.css imgs/logo.png #all other files It's important to include 1., 2. and 3. points only in index.html. Otherwise GET http://foo.bar/resource.ext net::ERR_FAILED occurs because every "child" file tries to cache the page while the page is already cached. In update_cache.js file I've put this code: function checkForUpdate() { if (window.applicationCache != undefined && window.applicationCache != null) { window.applicationCache.addEventListener('updateready', updateApplication); } } function updateApplication(event) { if (window.applicationCache.status != 4) return; window.applicationCache.removeEventListener('updateready', updateApplication); window.applicationCache.swapCache(); window.location.reload(); }
现在你只需更改文件,在manifest中你必须更新版本注释。现在访问index.html页面将更新缓存。
解决方案的部分不是我的,但我通过互联网找到了它们,并把它们放在一起,所以它是有效的。
我有一个案例,我将在线拍摄客户端的照片,如果照片被更改,就需要更新div。浏览器仍然显示着旧照片。因此,我使用了调用随机GET变量的方法,该变量每次都是唯一的。这就是它,如果它能帮助到任何人的话
<img src="/photos/userid_73.jpg?random=<?php echo rand() ?>" ...
编辑 正如其他人指出的那样,下面是更有效的解决方案,因为它只会在图像被更改时重新加载图像,通过文件大小识别这种更改:
<img src="/photos/userid_73.jpg?modified=<? filemtime("/photos/userid_73.jpg")?>"
有一个小技巧可以使用。诀窍是在脚本标记中向文件名附加一个参数/字符串,并在文件更改时更改它。
<script src=“myfile.js?version=1.0.0”></script>
浏览器将整个字符串解释为文件路径,即使“?”后面的是参数。所以现在发生的是,下次当你更新你的文件时,只要改变你网站上script标签中的数字(示例<script src="myfile.js?version=1.0.1"></script>),每个用户的浏览器都会看到文件已经改变,并抓取一个新的副本。