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

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


当前回答

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

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

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

其他回答

如果这是关于.css和.js的更改,那么一种方法是通过在每个版本的文件名后面附加类似“_versionNo”的东西来“缓存破坏”。例如:

script_1.0.css // This is the URL for release 1.0
script_1.1.css // This is the URL for release 1.1
script_1.2.css // etc.

或者在文件名后面:

script.css?v=1.0 // This is the URL for release 1.0
script.css?v=1.1 // This is the URL for release 1.1
script.css?v=1.2 // etc.

您可以检查这个链接,看看它是如何工作的。

我有一个案例,我将在线拍摄客户端的照片,如果照片被更改,就需要更新div。浏览器仍然显示着旧照片。因此,我使用了调用随机GET变量的方法,该变量每次都是唯一的。这就是它,如果它能帮助到任何人的话

<img src="/photos/userid_73.jpg?random=<?php echo rand() ?>" ...

编辑 正如其他人指出的那样,下面是更有效的解决方案,因为它只会在图像被更改时重新加载图像,通过文件大小识别这种更改:

<img src="/photos/userid_73.jpg?modified=<? filemtime("/photos/userid_73.jpg")?>"

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

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

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

对于webpack用户:-

我在webpack配置中添加了chunkash。这解决了每次部署时缓存失效的问题。我们还需要注意index.html/ asset。清单不是缓存在你的CDN或浏览器。在webpack配置中的块名配置将看起来像这样:-

文件名:[chunkhash] - $ {Date.now ()} . js

或者如果你使用contenthash那么

文件名:[contenthash] - $ {Date.now ()} . js

如果有兴趣,我已经找到了我的解决方案,让浏览器在。net MVC (.net fw 4.8)和使用包的情况下刷新.css和.js。 我想让浏览器仅在部署新程序集之后刷新缓存文件。

根据撒利亚多尼的回答,我的解决方案如下:

store your application base url in the web config app settings (the HttpContext is not yet available at runtime during the RegisterBundle...), then make this parameter changing according to the configuration (debug, staging, release...) by the xml transform In BundleConfig RegisterBundles get the assembly version by the means of reflection, and... ...change the default tag format of both styles and scripts so that the bundling system generates link and script tags appending a query string parameter on them.

下面是代码

public static void RegisterBundles(BundleCollection bundles)
{
   string baseUrl = system.Configuration.ConfigurationManager.AppSettings["by.app.base.url"].ToString();
       
   string assemblyVersion = Assembly.GetExecutingAssembly().GetName().Version.ToString();
   
   Styles.DefaultTagFormat = $"<link href='{baseUrl}{{0}}?v={assemblyVersion}' rel='stylesheet'/>";
   Scripts.DefaultTagFormat = $"<script src='{baseUrl}{{0}}?v={assemblyVersion}'></script>";
}

你会得到这样的标签

<script src="https://example.org/myscriptfilepath/script.js?v={myassemblyversion}"></script>

您只需要记住在部署之前构建一个新版本。

Ciao