Skip to content

Commit

Permalink
add more debugging (#3559)
Browse files Browse the repository at this point in the history
  • Loading branch information
peterpme authored Apr 3, 2023
1 parent b63c93b commit 5292080
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 5 deletions.
4 changes: 4 additions & 0 deletions packages/background/src/service-worker-loader.html
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@
const bodyElement = document.getElementsByTagName("body")[0];
bodyElement.appendChild(urlDiv);

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);
Expand All @@ -32,6 +34,8 @@
.split("background-scripts/")[1]
.split("/")[0];

console.log("hash", hash)

if (currentUrlHash !== hash) {
console.log("SERVICE WORKER: unregistering", registration);
await registration.unregister();
Expand Down
15 changes: 10 additions & 5 deletions packages/background/src/service-worker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,37 +5,42 @@ import { postMessageToIframe } from "./shared";
import { start } from ".";

let isStarted = false;
console.log("window:isStarted", isStarted);

self.addEventListener("install", async () => {
console.log("installing");
console.log("install:isStarted", isStarted);

if (!isStarted) {
start({ isMobile: true });
}

console.log("is mobile true, installed");

// 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) => {
console.log("activated");
console.log("activate:isStarted", isStarted);

// Override default behavior of service worker and claim the page without having to reload the page
console.log("activate:waitUntil...");
await event.waitUntil(clients.claim());
console.log("activating, claimed");
console.log("activate:claimed");

if (!isStarted) {
start({ isMobile: true });
isStarted = true;
}

console.log("activate:postMessageToIframe...");
await postMessageToIframe({ type: BACKGROUND_SERVICE_WORKER_READY });
console.log("activate:posted");
});

self.addEventListener("fetch", () => {
console.log("fetch:isStarted", isStarted);
// Start the service worker if it hasn't been started yet
if (!isStarted) {
start({ isMobile: true });
Expand Down

1 comment on commit 5292080

@vercel
Copy link

@vercel vercel bot commented on 5292080 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.