简体   繁体   English

Service Worker 忽略获取缓存设置

[英]Service worker ignores the fetch cache setting

On a web page I have:在 web 页面上,我有:

let response = await fetch(url, { cache: "no-store"});

Then in the service worker I have:然后在服务人员中我有:

function handleFetch(event) {
    console.log(event.request.cache)  // Always prints 'reload', should be 'no-store'
}
self.addEventListener("fetch", handleFetch);

I require cache to be set to no-store not reload , ie the value it was set to.我需要将缓存设置为no-store not reload ,即它设置的值。 Chrome ignores the value and always changes it to reload . Chrome 会忽略该值并始终将其更改为reload Am I doing something wrong or is this a case of Chrome inforcing its own value?我是在做错什么,还是 Chrome 强加了自己的价值?

Further more to prove cache set correctly on the request I tried:进一步证明根据我尝试的请求正确设置了缓存:

const init = { cache: "no-store"};
let request = new Request(url, init);
console.log(request.cache);  // Prints 'no-store'
let request = new Request(url, init);

I think this is a Chrome bug: https://bugs.chromium.org/p/chromium/issues/detail?id=1317680我认为这是一个 Chrome 错误: https://bugs.chromium.org/p/chromium/issues/detail?id=1317680

I guess I will just use a custom header or Cache-Control header until its sorted out.我想我只会使用自定义 header 或 Cache-Control header 直到它被整理出来。

From the MDN docs:来自 MDN 文档:

no-store — The browser fetches the resource from the remote server without first looking in the cache, and will not update the cache with the downloaded resource. no- store——浏览器从远程服务器获取资源而不首先查看缓存,并且不会用下载的资源更新缓存。
reload — The browser fetches the resource from the remote server without first looking in the cache, but then will update the cache with the downloaded resource.重新加载——浏览器从远程服务器获取资源而不首先查看缓存,但随后将使用下载的资源更新缓存。

https://developer.mozilla.org/en-US/docs/Web/API/Request/cache https://developer.mozilla.org/zh-CN/docs/Web/API/Request/cache

So it seems that Chrome wishes to cache everything internally, because reasons, it might be some internal optimisation, and because of that supersedes your cache settings with their own, but respecting your wishes that you wish to have the "freshest data" by changing it to reload.因此,Chrome 似乎希望在内部缓存所有内容,因为某些原因,这可能是一些内部优化,因此会用自己的缓存设置取代您的缓存设置,但尊重您希望通过更改它来获得“最新鲜数据”的愿望重新加载。

So you get the "newest uncached data", chrome gets to cache it for itself.所以你得到了“最新的未缓存数据”,chrome 会自己缓存它。 Best of both worlds.两全其美。

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

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