有没有一种方法可以让我把一些代码放在我的页面上,这样当有人访问一个网站时,它就会清空浏览器缓存,这样他们就可以查看更改了?
使用语言:ASP。净,VB。NET,当然还有HTML, CSS和jQuery。
有没有一种方法可以让我把一些代码放在我的页面上,这样当有人访问一个网站时,它就会清空浏览器缓存,这样他们就可以查看更改了?
使用语言:ASP。净,VB。NET,当然还有HTML, CSS和jQuery。
当前回答
不确定这是否真的对你有帮助,但这就是缓存在任何浏览器上的工作方式。当浏览器请求一个文件时,它应该总是向服务器发送请求,除非存在“脱机”模式。服务器将读取一些参数,如日期修改或标记。
服务器将为NOT MODIFIED返回一个304错误响应,浏览器将不得不使用它的缓存。如果标签在服务器端没有验证,或者修改的日期低于当前修改的日期,服务器应该返回带有新的修改日期或标签或两者的新内容。
如果没有缓存数据发送到浏览器,我猜行为是不确定的,浏览器可能会或可能不会缓存文件,不告诉他们是如何缓存的。如果您在响应中设置了缓存参数,它将正确缓存您的文件,然后服务器可能会选择返回一个304错误,或者返回新的内容。
这是应该做的。在url中使用随机参数或版本号更像一个黑客。
http://www.checkupdown.com/status/E304.html http://en.wikipedia.org/wiki/HTTP_ETag http://www.xpertdeveloper.com/2011/03/last-modified-header-vs-expire-header-vs-etag/
看了之后,我看到还有一个过期日期。如果你有问题,可能是因为你设置了过期日期。换句话说,当浏览器缓存你的文件时,因为它有一个有效期,所以它不应该在这个日期之前再次请求它。换句话说,它永远不会向服务器请求文件,也永远不会收到一个未修改的304。它将简单地使用缓存,直到达到过期日期或缓存被清除为止。
所以这是我的猜测,你有某种有效期,你应该使用最后修改标签或它们的混合,并确保没有过期日期。
如果人们倾向于频繁地刷新文件,而文件不会经常更改,那么设置一个大的过期日期可能是明智的。
我的2分钱!
其他回答
很多答案都忽略了一点——大多数开发人员都很清楚关闭缓存是低效的。然而,在许多常见的情况下,效率并不重要,默认缓存行为被严重破坏。
这些包括嵌套的、迭代的脚本测试(大的一个!)和坏的第三方软件变通方法。这里给出的解决方案都不足以解决这种常见场景。大多数web浏览器都有过于激进的缓存,并且没有提供合理的方法来避免这些问题。
如果有兴趣,我已经找到了我的解决方案,让浏览器在。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
我也遇到过类似的问题,我是这样解决的:
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页面将更新缓存。
解决方案的部分不是我的,但我通过互联网找到了它们,并把它们放在一起,所以它是有效的。
对于静态资源,正确的缓存是使用带有每个部署或文件版本值的查询参数。这将在每次部署后清除缓存。
/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" />
不要忘记更新程序集版本。
您是想清除缓存,还是只是确保当前(已更改的?)页面没有缓存?
如果是后者,那就应该如此简单
<META HTTP-EQUIV="Pragma" CONTENT="no-cache">