Skip to content

Commit

Permalink
start on message (#3564)
Browse files Browse the repository at this point in the history
  • Loading branch information
peterpme authored Apr 3, 2023
1 parent 4155400 commit e82541b
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 13 deletions.
6 changes: 3 additions & 3 deletions packages/background/src/service-worker-loader.html
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
13 changes: 3 additions & 10 deletions packages/background/src/service-worker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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) => {
Expand All @@ -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 () => {
Expand All @@ -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;
}
});

1 comment on commit e82541b

@vercel
Copy link

@vercel vercel bot commented on e82541b Apr 3, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.