From 99b1bf0ecbf1251691ce6be0a5433866314fa330 Mon Sep 17 00:00:00 2001 From: Tom Moor Date: Fri, 23 Apr 2021 12:31:27 -0700 Subject: [PATCH] fix: Avoid rare 'undefined is not a function' when attempting to register a server worker on Windows Chrome --- app/index.js | 26 +++++++++++++++++--------- 1 file changed, 17 insertions(+), 9 deletions(-) diff --git a/app/index.js b/app/index.js index de4c49ec5..4f59d24de 100644 --- a/app/index.js +++ b/app/index.js @@ -28,16 +28,24 @@ if (env.SENTRY_DSN) { if ("serviceWorker" in window.navigator) { window.addEventListener("load", () => { - window.navigator.serviceWorker - .register("/static/service-worker.js", { + // see: https://bugs.chromium.org/p/chromium/issues/detail?id=1097616 + // In some rare (<0.1% of cases) this call can return `undefined` + const maybePromise = window.navigator.serviceWorker.register( + "/static/service-worker.js", + { scope: "/", - }) - .then((registration) => { - console.log("SW registered: ", registration); - }) - .catch((registrationError) => { - console.log("SW registration failed: ", registrationError); - }); + } + ); + + if (maybePromise) { + maybePromise + .then((registration) => { + console.log("SW registered: ", registration); + }) + .catch((registrationError) => { + console.log("SW registration failed: ", registrationError); + }); + } }); }