combine all service-worker scripts into a single file to try and ensure included scripts aren't lost

This commit is contained in:
2024-02-14 20:46:34 -07:00
parent ca70b19831
commit f517b09ed7
8 changed files with 75 additions and 28 deletions

View File

@@ -1,5 +1,6 @@
/* eslint-env serviceworker */
/* global workbox */
/* eslint-disable *//* ... because old-browser-compatible files in this directory are combined into a single script during `npm run build` */
importScripts(
"https://storage.googleapis.com/workbox-cdn/releases/6.4.1/workbox-sw.js",
);
@@ -7,7 +8,9 @@ importScripts(
function logConsoleAndDb(message, arg1, arg2) {
// in chrome://serviceworker-internals note that the arg1 and arg2 here will show as "[object Object]" in that page but will show as expandable objects in the console
console.log(`${new Date().toISOString()} ${message}`, arg1, arg2);
if (self.appendDailyLog) {
// appendDailyLog is injected at build time by the vue.config.js configureWebpack apply plugin
// eslint-disable-next-line no-undef
if (appendDailyLog) {
let fullMessage = `${new Date().toISOString()} ${message}`;
if (arg1) {
fullMessage += `\n${JSON.stringify(arg1)}`;
@@ -15,25 +18,19 @@ function logConsoleAndDb(message, arg1, arg2) {
if (arg2) {
fullMessage += `\n${JSON.stringify(arg2)}`;
}
self.appendDailyLog(fullMessage);
// appendDailyLog is injected from safari-notifications.js at build time by the vue.config.js configureWebpack apply plugin
// eslint-disable-next-line no-undef
appendDailyLog(fullMessage);
} else {
// sometimes we get the error: "Uncaught TypeError: self.appendDailyLog is not a function"
// sometimes we get the error: "Uncaught TypeError: appendDailyLog is not a function"
console.log(
"Not logging to DB (often because self.appendDailyLog doesn't exist).",
"Not logging to DB (often because appendDailyLog doesn't exist).",
);
}
}
self.addEventListener("install", async (event) => {
console.log("Service worker got install event. Importing scripts...", event);
await importScripts(
"safari-notifications.js",
"nacl.js",
"noble-curves.js",
"noble-hashes.js",
);
// this should now be available
logConsoleAndDb("Service worker imported all scripts.");
self.addEventListener("install", async (/* event */) => {
logConsoleAndDb("Service worker finished installation.");
});
self.addEventListener("activate", (event) => {
@@ -84,7 +81,9 @@ self.addEventListener("push", function (event) {
} else {
title = payload.title || "Update";
}
message = await self.getNotificationCount();
// getNotificationCount is injected from safari-notifications.js at build time by the vue.config.js configureWebpack apply plugin
// eslint-disable-next-line no-undef
message = await getNotificationCount();
}
if (message) {
const options = {

View File

@@ -547,7 +547,11 @@ async function getNotificationCount() {
newClaims++;
}
if (newClaims > 0) {
result = `There are ${newClaims} new activities on Time Safari`;
if (newClaims === 1) {
result = "There is 1 new activity on Time Safari";
} else {
result = `There are ${newClaims} new activities on Time Safari`;
}
}
const most_recent_notified = claims[0]["id"];
await setMostRecentNotified(most_recent_notified);