From e82541b97a6f768d81fb04c492987eda75597439 Mon Sep 17 00:00:00 2001 From: Peter Piekarczyk Date: Mon, 3 Apr 2023 11:19:14 -0500 Subject: [PATCH] start on message (#3564) --- packages/background/src/service-worker-loader.html | 6 +++--- packages/background/src/service-worker.ts | 13 +++---------- 2 files changed, 6 insertions(+), 13 deletions(-) diff --git a/packages/background/src/service-worker-loader.html b/packages/background/src/service-worker-loader.html index b53f0afb1..f4c904f70 100644 --- a/packages/background/src/service-worker-loader.html +++ b/packages/background/src/service-worker-loader.html @@ -21,20 +21,20 @@ const bodyElement = document.getElementsByTagName("body")[0]; bodyElement.appendChild(urlDiv); - console.log("currentUrlHash", currentUrlHash) + console.log("currentUrlHash", currentUrlHash); // check to see if an old service worker needs to be unregistered const registrations = await navigator.serviceWorker.getRegistrations(); console.log("# SERVICE WORKERS", registrations.length); for (let registration of registrations) { - console.log("SERVICE WORKER: exists", registration); + console.log("SERVICE WORKER: exists", registration.scope); try { const hash = registration.scope .split("background-scripts/")[1] .split("/")[0]; - console.log("hash", hash) + console.log("hash", hash); if (currentUrlHash !== hash) { console.log("SERVICE WORKER: unregistering", registration); diff --git a/packages/background/src/service-worker.ts b/packages/background/src/service-worker.ts index 04e55d12f..2b1ce5b9c 100644 --- a/packages/background/src/service-worker.ts +++ b/packages/background/src/service-worker.ts @@ -10,15 +10,12 @@ console.log("window:isStarted", isStarted); self.addEventListener("install", async () => { console.log("install:isStarted", isStarted); - if (!isStarted) { - start({ isMobile: true }); - } + start({ isMobile: true }); // actives the current service worker immediately console.log("install:skipWaiting..."); await self.skipWaiting(); console.log("install:skipped..."); - isStarted = true; // called after skipWaiting to ensure its only called once }); self.addEventListener("activate", async (event) => { @@ -29,15 +26,11 @@ self.addEventListener("activate", async (event) => { await event.waitUntil(clients.claim()); console.log("activate:claimed"); - if (!isStarted) { - start({ isMobile: true }); - isStarted = true; - } - console.log("activate:postMessageToIframe..."); // This is most important line on mobile await postMessageToIframe({ type: BACKGROUND_SERVICE_WORKER_READY }); console.log("activate:posted"); + isStarted = true; }); self.addEventListener("fetch", async () => { @@ -46,7 +39,7 @@ self.addEventListener("fetch", async () => { // Start the service worker if it hasn't been started yet if (!isStarted) { - start({ isMobile: true }); + await postMessageToIframe({ type: BACKGROUND_SERVICE_WORKER_READY }); isStarted = true; } });