javascript 是什么导致无法在“ServiceWorkerGlobalScope”上执行“fetch”:“only-if-cached”只能设置为“same-origin”模式错误?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/48463483/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
What causes a Failed to execute 'fetch' on 'ServiceWorkerGlobalScope': 'only-if-cached' can be set only with 'same-origin' mode error?
提问by N. Dias
After upgrading to Chrome 64, I realized that this error appears when I load my page on a new tab.
升级到 Chrome 64 后,我意识到在新选项卡上加载页面时会出现此错误。
I can't identify where it is on the service worker. Here is my code to run the fetch:
我无法确定它在 Service Worker 上的位置。这是我运行提取的代码:
self.addEventListener('fetch', function(event) {
if (event.request.url.startsWith(self.location.origin)) {
event.respondWith(
caches.match(event.request).then(function(response) {
return response || fetch(event.request).then(function(fetch_resp){
return fetch_resp;
});
})
);
}
});
Could anyone here, who has more knowledge about service worker, help me to solve this error?
这里有谁对 Service Worker 有更多了解,可以帮我解决这个错误吗?
回答by rjbultitude
I believe this is a Chromium bug that has been reported here. Hopefully it will be fixed soon or some more information about the issue will be published.
我相信这是此处报告的 Chromium 错误。希望它会很快得到修复,或者有关该问题的更多信息将被发布。
Paul Irish implemented a temporary work around, which is as follows:
Paul Irish 实施了一项临时解决方案,如下所示:
if (e.request.cache === 'only-if-cached' && e.request.mode !== 'same-origin') {
return;
}
I ran it inside the callback for the service worker installand fetchlisteners and it prevented the error.
我在 service workerinstall和fetchlisteners的回调中运行它,它防止了错误。
You can see the full commit of Paul's code here.
您可以在此处查看 Paul 代码的完整提交。
回答by user3067740
perhaps the cache name is not unique from other applications, seems to fix the issue for me.
也许缓存名称与其他应用程序不是唯一的,似乎为我解决了这个问题。


