是什么导致 Failed to execute 'fetch' on 'ServiceWorkerGlobalScope': 'only-if-cached' can be set only with 'same-origin' mode 错误?

What causes a Failed to execute 'fetch' on 'ServiceWorkerGlobalScope': 'only-if-cached' can be set only with 'same-origin' mode error?

升级到 Chrome 64 后,我意识到在新选项卡上加载页面时会出现此错误。

我无法确定它在 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;
              });
           })
       );
   }
});

这里有谁对service worker有更多的了解,可以帮我解决这个错误吗?

我相信这是一个已报告的 Chromium 错误 here。希望它能尽快修复或发布有关该问题的更多信息。

Paul Irish 实施了一个临时解决方案,如下所示:

if (e.request.cache === 'only-if-cached' && e.request.mode !== 'same-origin') {
  return;
}

我 运行 它在 service worker installfetch 监听器的回调中,它阻止了错误。

你可以看到 full commit of Paul's code here.

也许缓存名称与其他应用程序不同,似乎可以解决我的问题。

对于那些再次搜索这个的人来说,当有人打开 devtools 时,它似乎在一段时间后再次弹出,你仍然可以看到问题中提到的错误。

在此处查看(新)错误报告:https://bugs.chromium.org/p/chromium/issues/detail?id=1098389

所以我们希望这个问题很快会再次得到修复!