From 85ba1676e549683b5eb376c9e173b6b763971545 Mon Sep 17 00:00:00 2001 From: Bruno Windels Date: Thu, 5 Nov 2020 22:51:21 +0100 Subject: [PATCH] log non-network errors in service worker to diagnose #187 --- src/platform/web/service-worker.template.js | 35 ++++++++++++--------- 1 file changed, 21 insertions(+), 14 deletions(-) diff --git a/src/platform/web/service-worker.template.js b/src/platform/web/service-worker.template.js index bc894b90..e3fa4651 100644 --- a/src/platform/web/service-worker.template.js +++ b/src/platform/web/service-worker.template.js @@ -86,22 +86,29 @@ function isCacheableThumbnail(url) { const baseURL = new URL(self.registration.scope); async function handleRequest(request) { - const url = new URL(request.url); - if (url.origin === baseURL.origin && url.pathname === baseURL.pathname) { - request = new Request(new URL("index.html", baseURL.href)); - } - let response = await readCache(request); - if (!response) { - // use cors so the resource in the cache isn't opaque and uses up to 7mb - // https://developers.google.com/web/tools/chrome-devtools/progressive-web-apps?utm_source=devtools#opaque-responses - if (isCacheableThumbnail(url)) { - response = await fetch(request, {mode: "cors", credentials: "omit"}); - } else { - response = await fetch(request); + try { + const url = new URL(request.url); + if (url.origin === baseURL.origin && url.pathname === baseURL.pathname) { + request = new Request(new URL("index.html", baseURL.href)); } - await updateCache(request, response); + let response = await readCache(request); + if (!response) { + // use cors so the resource in the cache isn't opaque and uses up to 7mb + // https://developers.google.com/web/tools/chrome-devtools/progressive-web-apps?utm_source=devtools#opaque-responses + if (isCacheableThumbnail(url)) { + response = await fetch(request, {mode: "cors", credentials: "omit"}); + } else { + response = await fetch(request); + } + await updateCache(request, response); + } + return response; + } catch (err) { + if (!(err instanceof TypeError)) { + console.error("error in service worker", err); + } + throw err; } - return response; } async function updateCache(request, response) {