Browse Source

add console logging for all service worker events

kb/add-usage-guide
Trent Larson 11 months ago
parent
commit
d6b1386741
  1. 23
      sw_scripts/additional-scripts.js

23
sw_scripts/additional-scripts.js

@ -5,7 +5,7 @@ importScripts(
); );
self.addEventListener("install", (event) => { self.addEventListener("install", (event) => {
console.log("Adding event listener for:", event); console.log(new Date().toISOString(), "Installing service worker:", event);
importScripts( importScripts(
"safari-notifications.js", "safari-notifications.js",
"nacl.js", "nacl.js",
@ -15,6 +15,7 @@ self.addEventListener("install", (event) => {
}); });
self.addEventListener("push", function (event) { self.addEventListener("push", function (event) {
console.log(new Date().toISOString(), "Received push event:", event);
event.waitUntil( event.waitUntil(
(async () => { (async () => {
try { try {
@ -24,42 +25,48 @@ self.addEventListener("push", function (event) {
} }
const message = await self.getNotificationCount(); const message = await self.getNotificationCount();
if (message) { if (message) {
console.log("Will notify user:", message); const title = payload && payload.title ? payload.title : "Message";
const title = payload ? payload.title : "Custom Title";
const options = { const options = {
body: message, body: message,
icon: payload ? payload.icon : "icon.png", icon: payload ? payload.icon : "icon.png",
badge: payload ? payload.badge : "badge.png", badge: payload ? payload.badge : "badge.png",
}; };
await self.registration.showNotification(title, options); await self.registration.showNotification(title, options);
console.log(new Date().toISOString(), "Notified user:", options);
} else { } else {
console.log("No notification message, so will not tell the user."); console.log(new Date().toISOString(), "No notification message.");
} }
} catch (error) { } 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) => { self.addEventListener("message", (event) => {
console.log(new Date().toISOString(), "Service worker message:", event);
if (event.data && event.data.type === "SEND_LOCAL_DATA") { if (event.data && event.data.type === "SEND_LOCAL_DATA") {
self.secret = event.data.data; self.secret = event.data.data;
event.ports[0].postMessage({ success: true }); event.ports[0].postMessage({ success: true });
} }
console.log(new Date().toISOString(), "Service worker posted message.");
}); });
self.addEventListener("activate", (event) => { 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()); event.waitUntil(clients.claim());
console.log("Service worker activated", event); console.log(new Date().toISOString(), "Service worker activated.");
}); });
self.addEventListener("fetch", (event) => { self.addEventListener("fetch", (event) => {
console.log("Got fetch event", event.request); console.log(new Date().toISOString(), "Got fetch event:", event);
}); });
self.addEventListener("error", (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("File:", event.filename);
console.error("Line:", event.lineno); console.error("Line:", event.lineno);
console.error("Column:", event.colno); console.error("Column:", event.colno);

Loading…
Cancel
Save