有没有一种方法可以让我把一些代码放在我的页面上,这样当有人访问一个网站时,它就会清空浏览器缓存,这样他们就可以查看更改了?

使用语言:ASP。净,VB。NET,当然还有HTML, CSS和jQuery。


当前回答

更新URL为以下工作为我:

/ custom.js吗?id = 1

通过在?id=后添加一个唯一的数字,并随着新的变化而递增,用户不必按CTRL + F5来刷新缓存。或者,您可以在?id=后附加当前时间或Epoch的哈希或字符串版本

id=1520606295

其他回答

您是想清除缓存,还是只是确保当前(已更改的?)页面没有缓存?

如果是后者,那就应该如此简单

<META HTTP-EQUIV="Pragma" CONTENT="no-cache">

对于静态资源,正确的缓存是使用带有每个部署或文件版本值的查询参数。这将在每次部署后清除缓存。

/Content/css/Site.css?version={FileVersionNumber}

这是ASP。NET MVC的例子。

<link href="@Url.Content("~/Content/Css/Reset.css")?version=@this.GetType().Assembly.GetName().Version" rel="stylesheet" type="text/css" />

不要忘记更新程序集版本。

我实现了这个简单的解决方案(还没有在生产环境中):

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更改到以前的版本将从缓存加载,那么这意味着缓存不会刷新,将所有以前的版本保存在缓存中)。

我是一个小新手,我很感激你的专业检查和审查,以确保我的方法是一个很好的方法。

希望能有所帮助。

很多答案都忽略了一点——大多数开发人员都很清楚关闭缓存是低效的。然而,在许多常见的情况下,效率并不重要,默认缓存行为被严重破坏。

这些包括嵌套的、迭代的脚本测试(大的一个!)和坏的第三方软件变通方法。这里给出的解决方案都不足以解决这种常见场景。大多数web浏览器都有过于激进的缓存,并且没有提供合理的方法来避免这些问题。

我也遇到过类似的问题,我是这样解决的:

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页面将更新缓存。

解决方案的部分不是我的,但我通过互联网找到了它们,并把它们放在一起,所以它是有效的。