简体   繁体   English

如何强制缓存的 web 页面资产在 2023 年重新加载

[英]How to force cached web page assets to reload in 2023

Old Cache Buster techniques I have found to date are:迄今为止我发现的旧 Cache Buster 技术是:

  1. Query String in the link src: /mstylesheet.css?cache_buster=12345链接 src 中的查询字符串:/mstylesheet.css?cache_buster=12345
  2. Change the filename each time: /mstylesheet-12345.css每次更改文件名:/mstylesheet-12345.css
  3. Apache: Cache-Control "must-revalidate" in conjunction with no-cache Apache:缓存控制“必须重新验证”与无缓存一起使用

I've noticed problems with all of these where stubborn Browser caches refuse to get updated assets?我注意到所有这些顽固的浏览器缓存拒绝获取更新资产的问题?

If I've understood correctly, the browser cache appears to treat the following URLs as completely different entities?如果我理解正确,浏览器缓存似乎将以下 URL 视为完全不同的实体?

  • /mstylesheet.css /mstylesheet.css
  • /mstylesheet.css?cache_buster=12345 /mstylesheet.css?cache_buster=12345
  • /mstylesheet.css?cache_buster=54321 /mstylesheet.css?cache_buster=54321

So the question is: Would the following javascript work to force an existing stylesheet, linked via a link tag, without a cache buster query string, to be updated in the browser cache?所以问题是:以下 javascript 是否会强制在浏览器缓存中更新通过链接标记链接、没有缓存破坏查询字符串的现有样式表?

fetch("/mstylesheet.css",{ method: "GET",headers: {"Cache-Control": "no-cache"}});

(I'd want to have this run a one off every now and then. Not on every page load). (我想让它时不时地运行一次。不是在每次加载页面时)。

From Referrer and cache control APIs for fetch() , you want either reload or no-store to bypass the locally stored cache:fetch() 的 Referrer 和缓存控制 API 中,您希望reloadno-store绕过本地存储的缓存:

  // Download a resource with cache busting, to bypass the cache
  // completely.
  fetch("some.json", {cache: "no-store"})
    .then(function(response) { /* consume the response */ });

  // Download a resource with cache busting, but update the HTTP
  // cache with the downloaded resource.
  fetch("some.json", {cache: "reload"})
    .then(function(response) { /* consume the response */ });

This is still valid in 2023. However, the real question is whether you want to support older browsers or older or misbehaving servers which may not support these directives.这在 2023 年仍然有效。但是,真正的问题是您是否要支持较旧的浏览器或可能不支持这些指令的较旧或行为不端的服务器。 A server can choose to ignore these headers or a service worker can choose to not forward your request to the server in the first place.服务器可以选择忽略这些标头,或者服务人员可以选择不首先将您的请求转发给服务器。 Presumably, you're in control of both of these, so they wouldn't be an issue, but it's still worth mentioning that these standards always rely on all parts of your app and stack using them correctly.据推测,您可以控制这两者,因此它们不会成为问题,但仍然值得一提的是,这些标准始终依赖于您应用程序的所有部分并正确使用它们。

If I've understood correctly, the browser cache appears to treat the following URLs as completely different entities?如果我理解正确,浏览器缓存似乎将以下 URL 视为完全不同的实体?

This is correct.这是对的。 At the very least, the full URL is used as the cache key.至少,完整的 URL 被用作缓存键。 Other things may be added to the key and Chrome implemented cache partitioning meaning that different origins usually don't share their cache, even if it's the exact same URL.其他内容可能会添加到密钥中,并且 Chrome 实现了缓存分区,这意味着不同来源通常不会共享其缓存,即使它是完全相同的 URL。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM