From d6b13867414d3553658f91c10b7edc9a62018e7c Mon Sep 17 00:00:00 2001 From: Trent Larson Date: Wed, 20 Dec 2023 19:49:04 -0700 Subject: [PATCH] add console logging for all service worker events --- sw_scripts/additional-scripts.js | 23 +++++++++++++++-------- 1 file changed, 15 insertions(+), 8 deletions(-) diff --git a/sw_scripts/additional-scripts.js b/sw_scripts/additional-scripts.js index 95edd0a..fe16c18 100644 --- a/sw_scripts/additional-scripts.js +++ b/sw_scripts/additional-scripts.js @@ -5,7 +5,7 @@ importScripts( ); self.addEventListener("install", (event) => { - console.log("Adding event listener for:", event); + console.log(new Date().toISOString(), "Installing service worker:", event); importScripts( "safari-notifications.js", "nacl.js", @@ -15,6 +15,7 @@ self.addEventListener("install", (event) => { }); self.addEventListener("push", function (event) { + console.log(new Date().toISOString(), "Received push event:", event); event.waitUntil( (async () => { try { @@ -24,42 +25,48 @@ self.addEventListener("push", function (event) { } const message = await self.getNotificationCount(); if (message) { - console.log("Will notify user:", message); - const title = payload ? payload.title : "Custom Title"; + const title = payload && payload.title ? payload.title : "Message"; const options = { body: message, icon: payload ? payload.icon : "icon.png", badge: payload ? payload.badge : "badge.png", }; await self.registration.showNotification(title, options); + console.log(new Date().toISOString(), "Notified user:", options); } else { - console.log("No notification message, so will not tell the user."); + console.log(new Date().toISOString(), "No notification message."); } } catch (error) { - console.error("Error processing the push event:", error); + console.error(new Date().toISOString(), "Error with push event", event, " - ", error); } })(), ); }); self.addEventListener("message", (event) => { + console.log(new Date().toISOString(), "Service worker message:", event); if (event.data && event.data.type === "SEND_LOCAL_DATA") { self.secret = event.data.data; event.ports[0].postMessage({ success: true }); } + console.log(new Date().toISOString(), "Service worker posted message."); }); self.addEventListener("activate", (event) => { + console.log(new Date().toISOString(), "Service worker activating...", event); + // see https://developer.mozilla.org/en-US/docs/Web/API/Clients/claim + // and https://web.dev/articles/service-worker-lifecycle#clientsclaim event.waitUntil(clients.claim()); - console.log("Service worker activated", event); + console.log(new Date().toISOString(), "Service worker activated."); }); self.addEventListener("fetch", (event) => { - console.log("Got fetch event", event.request); + console.log(new Date().toISOString(), "Got fetch event:", event); }); self.addEventListener("error", (event) => { - console.error("Error in Service Worker:", event.message); + console.error(new Date().toISOString(), "Error in Service Worker:", event); + console.error("Message:", event.message); console.error("File:", event.filename); console.error("Line:", event.lineno); console.error("Column:", event.colno);