|
|
@ -4,8 +4,22 @@ importScripts( |
|
|
|
"https://storage.googleapis.com/workbox-cdn/releases/6.4.1/workbox-sw.js", |
|
|
|
); |
|
|
|
|
|
|
|
function logDbOrConsole(self, message, arg1, arg2) { |
|
|
|
let fullMessage = `${new Date().toISOString()} ${message}`; |
|
|
|
if (arg1) { |
|
|
|
fullMessage += `: ${JSON.stringify(arg1)}`; |
|
|
|
} |
|
|
|
if (arg2) { |
|
|
|
fullMessage += ` -- ${JSON.stringify(arg2)}`; |
|
|
|
} |
|
|
|
const logged = self.logMessage(fullMessage); |
|
|
|
if (!logged) { |
|
|
|
console.log(`$new Date().toISOString()} ${message}`, arg1, arg2); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
self.addEventListener("install", (event) => { |
|
|
|
console.log(new Date().toISOString(), "Installing service worker:", event); |
|
|
|
logDbOrConsole(self, "Installing service worker:", event); |
|
|
|
importScripts( |
|
|
|
"safari-notifications.js", |
|
|
|
"nacl.js", |
|
|
@ -15,7 +29,7 @@ self.addEventListener("install", (event) => { |
|
|
|
}); |
|
|
|
|
|
|
|
self.addEventListener("push", function (event) { |
|
|
|
console.log(new Date().toISOString(), "Received push event:", event); |
|
|
|
logDbOrConsole(self, "Received push event:", event); |
|
|
|
event.waitUntil( |
|
|
|
(async () => { |
|
|
|
try { |
|
|
@ -32,40 +46,41 @@ self.addEventListener("push", function (event) { |
|
|
|
badge: payload ? payload.badge : "badge.png", |
|
|
|
}; |
|
|
|
await self.registration.showNotification(title, options); |
|
|
|
console.log(new Date().toISOString(), "Notified user:", options); |
|
|
|
logDbOrConsole(self, "Notified user:", options); |
|
|
|
} else { |
|
|
|
console.log(new Date().toISOString(), "No notification message."); |
|
|
|
logDbOrConsole(self, "No notification message."); |
|
|
|
} |
|
|
|
} catch (error) { |
|
|
|
console.error(new Date().toISOString(), "Error with push event", event, " - ", error); |
|
|
|
logDbOrConsole(self, "Error with push event", event, error); |
|
|
|
} |
|
|
|
})(), |
|
|
|
); |
|
|
|
}); |
|
|
|
|
|
|
|
self.addEventListener("message", (event) => { |
|
|
|
console.log(new Date().toISOString(), "Service worker message:", event); |
|
|
|
logDbOrConsole(self, "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."); |
|
|
|
logDbOrConsole(self, "Service worker posted message."); |
|
|
|
}); |
|
|
|
|
|
|
|
self.addEventListener("activate", (event) => { |
|
|
|
console.log(new Date().toISOString(), "Service worker activating...", event); |
|
|
|
logDbOrConsole(self, "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(new Date().toISOString(), "Service worker activated."); |
|
|
|
logDbOrConsole(self, "Service worker activated."); |
|
|
|
}); |
|
|
|
|
|
|
|
self.addEventListener("fetch", (event) => { |
|
|
|
console.log(new Date().toISOString(), "Got fetch event:", event); |
|
|
|
logDbOrConsole(self, "Got fetch event:", event); |
|
|
|
}); |
|
|
|
|
|
|
|
self.addEventListener("error", (event) => { |
|
|
|
console.error(new Date().toISOString(), "Error in Service Worker:", event); |
|
|
|
logDbOrConsole(self, "Error in Service Worker:", event); |
|
|
|
console.error("Full Error:", event); |
|
|
|
console.error("Message:", event.message); |
|
|
|
console.error("File:", event.filename); |
|
|
|
console.error("Line:", event.lineno); |
|
|
|