Browse Source

add DB logging for service-worker events

pull/100/head
Trent Larson 11 months ago
parent
commit
f8002c4550
  1. 37
      sw_scripts/additional-scripts.js
  2. 25
      sw_scripts/safari-notifications.js

37
sw_scripts/additional-scripts.js

@ -4,8 +4,22 @@ importScripts(
"https://storage.googleapis.com/workbox-cdn/releases/6.4.1/workbox-sw.js", "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) => { self.addEventListener("install", (event) => {
console.log(new Date().toISOString(), "Installing service worker:", event); logDbOrConsole(self, "Installing service worker:", event);
importScripts( importScripts(
"safari-notifications.js", "safari-notifications.js",
"nacl.js", "nacl.js",
@ -15,7 +29,7 @@ self.addEventListener("install", (event) => {
}); });
self.addEventListener("push", function (event) { self.addEventListener("push", function (event) {
console.log(new Date().toISOString(), "Received push event:", event); logDbOrConsole(self, "Received push event:", event);
event.waitUntil( event.waitUntil(
(async () => { (async () => {
try { try {
@ -32,40 +46,41 @@ self.addEventListener("push", function (event) {
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); logDbOrConsole(self, "Notified user:", options);
} else { } else {
console.log(new Date().toISOString(), "No notification message."); logDbOrConsole(self, "No notification message.");
} }
} catch (error) { } 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) => { 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") { 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."); logDbOrConsole(self, "Service worker posted message.");
}); });
self.addEventListener("activate", (event) => { 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 // see https://developer.mozilla.org/en-US/docs/Web/API/Clients/claim
// and https://web.dev/articles/service-worker-lifecycle#clientsclaim // and https://web.dev/articles/service-worker-lifecycle#clientsclaim
event.waitUntil(clients.claim()); event.waitUntil(clients.claim());
console.log(new Date().toISOString(), "Service worker activated."); logDbOrConsole(self, "Service worker activated.");
}); });
self.addEventListener("fetch", (event) => { self.addEventListener("fetch", (event) => {
console.log(new Date().toISOString(), "Got fetch event:", event); logDbOrConsole(self, "Got fetch event:", event);
}); });
self.addEventListener("error", (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("Message:", event.message);
console.error("File:", event.filename); console.error("File:", event.filename);
console.error("Line:", event.lineno); console.error("Line:", event.lineno);

25
sw_scripts/safari-notifications.js

@ -404,6 +404,27 @@ async function setMostRecentNotified(id) {
} }
} }
async function logMessage(message) {
try {
const db = await openIndexedDB("TimeSafari");
const transaction = db.transaction("worker_log", "readwrite");
const store = transaction.objectStore("worker_log");
// will only keep one day's worth of logs
let data = await getRecord(store, new Date().toDateString());
if (!data) {
await store.clear(); // clear out anything older than today
}
data = data || "";
data += `\n${message}`;
await updateRecord(store, data);
transaction.oncomplete = () => db.close();
return true;
} catch (error) {
console.error("IndexedDB logMessage error:", error);
return false;
}
}
function openIndexedDB(dbName) { function openIndexedDB(dbName) {
return new Promise((resolve, reject) => { return new Promise((resolve, reject) => {
const request = indexedDB.open(dbName); const request = indexedDB.open(dbName);
@ -420,6 +441,7 @@ function getRecord(store, key) {
}); });
} }
// Note that this assumes there is only one record in the store.
function updateRecord(store, data) { function updateRecord(store, data) {
return new Promise((resolve, reject) => { return new Promise((resolve, reject) => {
const request = store.put(data); const request = store.put(data);
@ -437,6 +459,9 @@ async function fetchAllAccounts() {
if (!db.objectStoreNames.contains("accounts")) { if (!db.objectStoreNames.contains("accounts")) {
db.createObjectStore("accounts", { keyPath: "id" }); db.createObjectStore("accounts", { keyPath: "id" });
} }
if (!db.objectStoreNames.contains("worker_log")) {
db.createObjectStore("worker_log");
}
}; };
openRequest.onsuccess = function (event) { openRequest.onsuccess = function (event) {

Loading…
Cancel
Save