From f517b09ed715c653df7249148f61bef5590d56a1 Mon Sep 17 00:00:00 2001 From: Trent Larson Date: Wed, 14 Feb 2024 20:46:34 -0700 Subject: [PATCH] combine all service-worker scripts into a single file to try and ensure included scripts aren't lost --- .gitignore | 2 ++ README.md | 6 ------ project.task.yaml | 8 ++++---- src/registerServiceWorker.ts | 2 +- sw_combine.js | 29 +++++++++++++++++++++++++++++ sw_scripts/additional-scripts.js | 29 ++++++++++++++--------------- sw_scripts/safari-notifications.js | 6 +++++- vue.config.js | 21 ++++++++++++++++++++- 8 files changed, 75 insertions(+), 28 deletions(-) create mode 100644 sw_combine.js diff --git a/.gitignore b/.gitignore index 137d6788..68fbf3bf 100644 --- a/.gitignore +++ b/.gitignore @@ -2,6 +2,8 @@ node_modules /dist signature.bin +# generated during `npm run build` +sw_scripts-combined.js *.pem verified.txt myenv diff --git a/README.md b/README.md index adc09e2a..fa0569d4 100644 --- a/README.md +++ b/README.md @@ -42,12 +42,6 @@ npm run lint * `npm run build` -...to make sure the service worker scripts are in proper form. (It's only important if you changed something in that directory.) - -* `cp sw_scripts/[ns]* dist/` - -... to copy the contents of the `sw_scripts` folder to the `dist` folder - except additional_scripts.js. - * Get on the server and back up the time-safari folder. * `rsync -azvu -e "ssh -i ~/.ssh/..." dist ubuntutest@test.timesafari.app:time-safari` diff --git a/project.task.yaml b/project.task.yaml index 0ecac4a7..6a692cfb 100644 --- a/project.task.yaml +++ b/project.task.yaml @@ -1,17 +1,16 @@ tasks : -- 02 error passing control of a project to another user - -- 08 notifications for feed - make them reliable : - - leverage different messaging platform? +- 01 release server & client for fix to project-editing - .5 fix timeSafari.org cert renewals - .2 anchor hash into BTC +- .1 add step 1 to onboarding hints to "install" - 01 bookmarks for BVC - 24 compelling UI for credential presentations + - discover who in my network has activity on a project - 24 compelling UI for statistics (eg. World?) - .5 stop from seeing an error on the first page when browser doesn't support service workers (which I've seen on iPhone; visible in Firefox private window) @@ -28,6 +27,7 @@ tasks : - 24 make the contact browsing on the front page something that invites more action +- .2 list the "show more" contacts alphabetically - .5 change server plan & project endpoints to use jwtId as identifier rather than rowid - 16 edit offers & gives, or revoke allowing re-creation - .1 When available in the server, give message for 'nonAmountGiven' on offers on ProjectsView page. diff --git a/src/registerServiceWorker.ts b/src/registerServiceWorker.ts index 22ca9b9a..56f73219 100644 --- a/src/registerServiceWorker.ts +++ b/src/registerServiceWorker.ts @@ -3,7 +3,7 @@ import { register } from "register-service-worker"; if (process.env.NODE_ENV === "production") { - register("/additional-scripts.js", { + register("/sw_scripts-combined.js", { ready() { console.log( "App is being served from cache by a service worker.\n" + diff --git a/sw_combine.js b/sw_combine.js new file mode 100644 index 00000000..67761f3e --- /dev/null +++ b/sw_combine.js @@ -0,0 +1,29 @@ +/** + * We've seen cases where the functions inside safari-notifications.js are not found. + * This is our attempt to ensure that all the functions are available. + */ + +const fs = require("fs"); +const path = require("path"); + +const swScriptsDir = path.resolve(__dirname, "sw_scripts"); +const outputFile = path.resolve(__dirname, "sw_scripts-combined.js"); + +// Read all files in the sw_scripts directory +fs.readdir(swScriptsDir, (err, files) => { + if (err) { + console.error("Error reading directory:", err); + return; + } + + // Combine files content into one script + const combinedContent = files + .filter((file) => path.extname(file) === ".js") + .map((file) => fs.readFileSync(path.join(swScriptsDir, file), "utf8")) + .join("\n"); + + // Write the combined content to the output file + fs.writeFileSync(outputFile, combinedContent, "utf8"); + + console.log("Service worker files combined."); +}); diff --git a/sw_scripts/additional-scripts.js b/sw_scripts/additional-scripts.js index 87b01728..3051b927 100644 --- a/sw_scripts/additional-scripts.js +++ b/sw_scripts/additional-scripts.js @@ -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 = { diff --git a/sw_scripts/safari-notifications.js b/sw_scripts/safari-notifications.js index de69c3eb..7803e6f0 100644 --- a/sw_scripts/safari-notifications.js +++ b/sw_scripts/safari-notifications.js @@ -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); diff --git a/vue.config.js b/vue.config.js index 946e889b..ea4ea006 100644 --- a/vue.config.js +++ b/vue.config.js @@ -1,5 +1,6 @@ const { defineConfig } = require("@vue/cli-service"); const { gitDescribeSync } = require("git-describe"); +const { exec } = require("child_process"); process.env.VUE_APP_GIT_HASH = gitDescribeSync().hash; @@ -10,6 +11,23 @@ module.exports = defineConfig({ experiments: { topLevelAwait: true, }, + plugins: [ + { + // Still don't know why this runs three times. + apply: (compiler) => { + compiler.hooks.beforeCompile.tap("BeforeCompile", () => { + // Execute combine-sw.js script + exec("node sw_combine.js", (error, stdout, stderr) => { + if (error || stderr) { + console.error("Service worker files error:", error || stderr); + } else { + console.log("Finished combining service worker files.", stdout); + } + }); + }); + }, + }, + ], }, pwa: { iconPaths: { @@ -17,7 +35,8 @@ module.exports = defineConfig({ }, workboxPluginMode: "InjectManifest", workboxOptions: { - swSrc: "./sw_scripts/additional-scripts.js", + // this script will be checked for linting (sw_scripts/* files generate about 1000 linting errors) + swSrc: "./sw_scripts-combined.js", }, }, });