有没有一种方法可以让我把一些代码放在我的页面上,这样当有人访问一个网站时,它就会清空浏览器缓存,这样他们就可以查看更改了?
使用语言:ASP。净,VB。NET,当然还有HTML, CSS和jQuery。
有没有一种方法可以让我把一些代码放在我的页面上,这样当有人访问一个网站时,它就会清空浏览器缓存,这样他们就可以查看更改了?
使用语言:ASP。净,VB。NET,当然还有HTML, CSS和jQuery。
当前回答
更新URL为以下工作为我:
/ custom.js吗?id = 1
通过在?id=后添加一个唯一的数字,并随着新的变化而递增,用户不必按CTRL + F5来刷新缓存。或者,您可以在?id=后附加当前时间或Epoch的哈希或字符串版本
id=1520606295
其他回答
更新2012
这是一个老问题,但我认为它需要一个更最新的答案,因为现在有一种方法可以更好地控制网站缓存。
在离线Web应用程序(实际上是任何HTML5网站)中,applicationCache.swapCache()可以用来更新网站的缓存版本,而不需要手动重新加载页面。
这是一个来自HTML5 Rocks上使用应用程序缓存的初学者指南的代码示例,解释了如何将用户更新到站点的最新版本:
// Check if a new cache is available on page load.
window.addEventListener('load', function(e) {
window.applicationCache.addEventListener('updateready', function(e) {
if (window.applicationCache.status == window.applicationCache.UPDATEREADY) {
// Browser downloaded a new app cache.
// Swap it in and reload the page to get the new hotness.
window.applicationCache.swapCache();
if (confirm('A new version of this site is available. Load it?')) {
window.location.reload();
}
} else {
// Manifest didn't changed. Nothing new to server.
}
}, false);
}, false);
有关更多信息,请参见在Mozilla开发者网络上使用应用程序缓存。
更新2016
网络上的事物变化很快。 这个问题是在2009年提出的,在2012年,我发布了一篇关于处理问题中描述的问题的新方法的更新。又过了4年,现在看来它已经被弃用了。感谢cgaldiolo在评论中指出这一点。
目前,截至2016年7月,HTML标准,第7.9节,脱机Web应用程序包括一个弃用警告:
这个特性正在从Web平台上移除。 (这是一个漫长的过程,需要很多年。)使用任何 此时非常不鼓励使用离线Web应用程序特性。 使用服务工作者代替。
在Mozilla开发者网络上使用应用程序缓存也是如此,我在2012年引用过:
已弃用该特性已从Web标准中移除。 虽然一些浏览器可能仍然支持它,但它仍在开发过程中 被删除。不要在旧的或新的项目中使用它。页面或Web应用程序 使用它可能会随时损坏。
请参见Bug 1204581 -如果启用service worker获取拦截,为AppCache添加弃用通知。
查看缓存控制和过期的META标签。
< meta http-equiv =" cache-control " content =" no-cache "> <META HTTP-EQUIV="EXPIRES" CONTENT="Mon, 22 july 2002 11:12:01 GMT">
另一个常见的做法是在请求文件的末尾附加不断变化的字符串。例如:
<脚本类型=“文本/javascript”“src=”主。js?v = 12392823 " > < /脚本>
如果有兴趣,我已经找到了我的解决方案,让浏览器在。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
我有同样的问题,我所做的就是改变文件名链接到我的index.html文件,然后进入index.html文件,更新他们的名字,不是最好的做法,但如果它工作。浏览器将它们视为新文件,因此它们会被重新下载到用户的设备上。
例子: 我想更新一个css文件,它的命名为styles.css,把它改成styles.css
进入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.
您可以检查这个链接,看看它是如何工作的。