From 0090371cdcdac0e5600415c181131ec2f5c5e217 Mon Sep 17 00:00:00 2001 From: Robert Knight Date: Tue, 23 Apr 2024 16:36:24 +0100 Subject: [PATCH 01/14] Add first draft of service worker --- Makefile | 2 +- src/simulator.ts | 16 ++++++++++++ src/sw.js | 68 ++++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 85 insertions(+), 1 deletion(-) create mode 100644 src/sw.js diff --git a/Makefile b/Makefile index 60df0f9f..e054de9f 100644 --- a/Makefile +++ b/Makefile @@ -8,7 +8,7 @@ build: dist: build mkdir -p $(BUILD)/build - cp -r $(SRC)/*.html $(SRC)/term.js src/examples $(BUILD) + cp -r $(SRC)/*.html $(SRC)/term.js src/examples $(SRC)/sw.js $(BUILD) cp $(SRC)/build/firmware.js $(SRC)/build/simulator.js $(SRC)/build/firmware.wasm $(BUILD)/build/ watch: dist diff --git a/src/simulator.ts b/src/simulator.ts index c45f60e3..73db57a1 100644 --- a/src/simulator.ts +++ b/src/simulator.ts @@ -15,6 +15,22 @@ declare global { } } +function initServiceWorker() { + if ("serviceWorker" in navigator) { + window.addEventListener("load", () => { + navigator.serviceWorker.register("sw.js").then( + function (_registration) { + console.log("Simulator ServiceWorker registration successful"); + }, + function (err) { + console.log("Simulator ServiceWorker registration failed: ", err); + } + ); + }); + } +} + +initServiceWorker(); const fs = new FileSystem(); const board = createBoard(new Notifications(window.parent), fs); window.addEventListener("message", createMessageListener(board)); diff --git a/src/sw.js b/src/sw.js new file mode 100644 index 00000000..c9ccea53 --- /dev/null +++ b/src/sw.js @@ -0,0 +1,68 @@ +function initSimulatorServiceWorker() { + const simUrls = ["simulator.html", "build/simulator.js", "build/firmware.js"]; + let didInstall = false; + const cacheName = "simulator"; + + self.addEventListener("install", function (ev) { + didInstall = true; + console.log("Installing service worker..."); + ev.waitUntil( + caches + .open(cacheName) + .then(function (cache) { + console.log("Opened cache"); + return cache.addAll(simUrls); + }) + .then(function () { + return self.skipWaiting(); + }) + ); + }); + + self.addEventListener("activate", function (ev) { + console.log("Activating service worker..."); + ev.waitUntil( + caches + .keys() + .then(function (_cacheNames) { + // Delete old versions in cache here. + }) + .then(function () { + if (didInstall) { + // Only notify clients for the first activation + didInstall = false; + // Necessary? + return notifyAllClientsAsync(); + } + return Promise.resolve(); + }) + ); + }); + + self.addEventListener("fetch", function (ev) { + ev.respondWith( + caches.match(ev.request).then(function (response) { + return response || fetch(ev.request); + }) + ); + }); + + function notifyAllClientsAsync() { + var scope = self; + return scope.clients + .claim() + .then(function () { + return scope.clients.matchAll(); + }) + .then(function (clients) { + clients.forEach(function (client) { + return client.postMessage({ + type: "serviceworker", + state: "activated", + }); + }); + }); + } +} + +initSimulatorServiceWorker(); From 538355fd447f10b23e1e926a00e6028ed578c289 Mon Sep 17 00:00:00 2001 From: Robert Knight Date: Wed, 24 Apr 2024 13:46:50 +0100 Subject: [PATCH 02/14] Update service worker, add version control --- src/simulator.ts | 20 +++++---- src/sw.js | 104 +++++++++++++++++++---------------------------- 2 files changed, 53 insertions(+), 71 deletions(-) diff --git a/src/simulator.ts b/src/simulator.ts index 73db57a1..b33c894e 100644 --- a/src/simulator.ts +++ b/src/simulator.ts @@ -16,18 +16,22 @@ declare global { } function initServiceWorker() { - if ("serviceWorker" in navigator) { - window.addEventListener("load", () => { + window.addEventListener("load", () => { + if ("serviceWorker" in navigator) { navigator.serviceWorker.register("sw.js").then( - function (_registration) { - console.log("Simulator ServiceWorker registration successful"); + (registration) => { + console.log("Simulator service worker registration successful"); }, - function (err) { - console.log("Simulator ServiceWorker registration failed: ", err); + (error) => { + console.error( + `Simulator service worker registration failed: ${error}` + ); } ); - }); - } + } else { + console.error("Service workers are not supported."); + } + }); } initServiceWorker(); diff --git a/src/sw.js b/src/sw.js index c9ccea53..a5da9f5b 100644 --- a/src/sw.js +++ b/src/sw.js @@ -1,68 +1,46 @@ -function initSimulatorServiceWorker() { - const simUrls = ["simulator.html", "build/simulator.js", "build/firmware.js"]; - let didInstall = false; - const cacheName = "simulator"; +const version = "v0.0.1"; +const assets = ["simulator.html", "build/simulator.js", "build/firmware.js"]; +const cacheName = `simulator-${version}`; - self.addEventListener("install", function (ev) { - didInstall = true; - console.log("Installing service worker..."); - ev.waitUntil( - caches - .open(cacheName) - .then(function (cache) { - console.log("Opened cache"); - return cache.addAll(simUrls); - }) - .then(function () { - return self.skipWaiting(); - }) - ); - }); +self.addEventListener("install", (event) => { + console.log("Installing simulator service worker..."); + event.waitUntil( + (async () => { + const cache = await caches.open(cacheName); + await cache.addAll(assets); + self.skipWaiting(); + })() + ); +}); - self.addEventListener("activate", function (ev) { - console.log("Activating service worker..."); - ev.waitUntil( - caches - .keys() - .then(function (_cacheNames) { - // Delete old versions in cache here. - }) - .then(function () { - if (didInstall) { - // Only notify clients for the first activation - didInstall = false; - // Necessary? - return notifyAllClientsAsync(); +self.addEventListener("activate", (event) => { + console.log("Activating simulator service worker..."); + event.waitUntil( + (async () => { + const names = await caches.keys(); + await Promise.all( + names.map((name) => { + if (name !== cacheName) { + return caches.delete(name); } - return Promise.resolve(); }) - ); - }); - - self.addEventListener("fetch", function (ev) { - ev.respondWith( - caches.match(ev.request).then(function (response) { - return response || fetch(ev.request); - }) - ); - }); - - function notifyAllClientsAsync() { - var scope = self; - return scope.clients - .claim() - .then(function () { - return scope.clients.matchAll(); - }) - .then(function (clients) { - clients.forEach(function (client) { - return client.postMessage({ - type: "serviceworker", - state: "activated", - }); - }); - }); - } -} + ); + await clients.claim(); + })() + ); +}); -initSimulatorServiceWorker(); +self.addEventListener("fetch", (event) => { + event.respondWith( + (async () => { + const cachedResponse = await caches.match(event.request); + if (cachedResponse) { + return cachedResponse; + } + const response = await fetch(event.request); + const cache = await caches.open(cacheName); + cache.put(event.request, response.clone()); + return response; + })() + ); +}); From 4c724eb5a1be0705583bc67928a594493ec896ef Mon Sep 17 00:00:00 2001 From: Robert Knight Date: Thu, 25 Apr 2024 10:43:39 +0100 Subject: [PATCH 03/14] Service worker tweaks --- src/simulator.ts | 2 +- src/sw.js | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/src/simulator.ts b/src/simulator.ts index b33c894e..411fc79b 100644 --- a/src/simulator.ts +++ b/src/simulator.ts @@ -19,7 +19,7 @@ function initServiceWorker() { window.addEventListener("load", () => { if ("serviceWorker" in navigator) { navigator.serviceWorker.register("sw.js").then( - (registration) => { + (_registration) => { console.log("Simulator service worker registration successful"); }, (error) => { diff --git a/src/sw.js b/src/sw.js index a5da9f5b..73264ecd 100644 --- a/src/sw.js +++ b/src/sw.js @@ -1,14 +1,14 @@ -const version = "v0.0.1"; +const version = "v0.0.2"; const assets = ["simulator.html", "build/simulator.js", "build/firmware.js"]; const cacheName = `simulator-${version}`; self.addEventListener("install", (event) => { console.log("Installing simulator service worker..."); + self.skipWaiting(); event.waitUntil( (async () => { const cache = await caches.open(cacheName); await cache.addAll(assets); - self.skipWaiting(); })() ); }); From 07b703ba7613ecaaf673428e3e6517edfd883938 Mon Sep 17 00:00:00 2001 From: Robert Knight Date: Thu, 25 Apr 2024 13:08:56 +0100 Subject: [PATCH 04/14] Auto reload the page when new service worker is installed --- src/simulator.ts | 16 +++++++++++++++- src/sw.js | 2 +- 2 files changed, 16 insertions(+), 2 deletions(-) diff --git a/src/simulator.ts b/src/simulator.ts index 411fc79b..5d3ebf10 100644 --- a/src/simulator.ts +++ b/src/simulator.ts @@ -19,8 +19,22 @@ function initServiceWorker() { window.addEventListener("load", () => { if ("serviceWorker" in navigator) { navigator.serviceWorker.register("sw.js").then( - (_registration) => { + (registration) => { console.log("Simulator service worker registration successful"); + // Reload the page when a new service worker is installed. + registration.onupdatefound = function () { + const installingWorker = registration.installing; + if (installingWorker) { + installingWorker.onstatechange = function () { + if ( + installingWorker.state === "installed" && + navigator.serviceWorker.controller + ) { + window.location.reload(); + } + }; + } + }; }, (error) => { console.error( diff --git a/src/sw.js b/src/sw.js index 73264ecd..0ef355d7 100644 --- a/src/sw.js +++ b/src/sw.js @@ -1,4 +1,4 @@ -const version = "v0.0.2"; +const version = "v0.0.3"; const assets = ["simulator.html", "build/simulator.js", "build/firmware.js"]; const cacheName = `simulator-${version}`; From 7075a41ab7368f7c1a92b9f319425e209a5717e9 Mon Sep 17 00:00:00 2001 From: Robert Knight Date: Tue, 30 Apr 2024 11:50:17 +0100 Subject: [PATCH 05/14] Bump GitHub actions versions --- .github/workflows/build.yml | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/.github/workflows/build.yml b/.github/workflows/build.yml index cba9aca9..232f7a5f 100644 --- a/.github/workflows/build.yml +++ b/.github/workflows/build.yml @@ -26,9 +26,9 @@ jobs: steps: # Note: This workflow will not run on forks without modification; we're open to making steps # that rely on our deployment infrastructure conditional. Please open an issue. - - uses: actions/checkout@v3 + - uses: actions/checkout@v4 - name: Configure node - uses: actions/setup-node@v3 + uses: actions/setup-node@v4 with: node-version: 20.x cache: "npm" From d74a44fe4ffe80285e10175183abcfd86b1377d9 Mon Sep 17 00:00:00 2001 From: Robert Knight Date: Tue, 30 Apr 2024 11:50:40 +0100 Subject: [PATCH 06/14] Service worker in TypeScript with versioning --- Makefile | 2 +- src/Makefile | 1 + src/{sw.js => sw.ts} | 9 +++++++-- tsconfig.json | 2 +- 4 files changed, 10 insertions(+), 4 deletions(-) rename src/{sw.js => sw.ts} (81%) diff --git a/Makefile b/Makefile index e054de9f..85776152 100644 --- a/Makefile +++ b/Makefile @@ -8,7 +8,7 @@ build: dist: build mkdir -p $(BUILD)/build - cp -r $(SRC)/*.html $(SRC)/term.js src/examples $(SRC)/sw.js $(BUILD) + cp -r $(SRC)/*.html $(SRC)/term.js src/examples $(SRC)/build/sw.js $(BUILD) cp $(SRC)/build/firmware.js $(SRC)/build/simulator.js $(SRC)/build/firmware.wasm $(BUILD)/build/ watch: dist diff --git a/src/Makefile b/src/Makefile index 66dbdcba..f2f7de17 100644 --- a/src/Makefile +++ b/src/Makefile @@ -147,6 +147,7 @@ $(BUILD)/micropython.js: $(OBJ) jshal.js simulator-js simulator-js: npx esbuild ./simulator.ts --bundle --outfile=$(BUILD)/simulator.js --loader:.svg=text + npx esbuild --define:process.env.version=$$(cat ../package.json | jq .version) ./sw.ts --bundle --outfile=$(BUILD)/sw.js include $(TOP)/py/mkrules.mk diff --git a/src/sw.js b/src/sw.ts similarity index 81% rename from src/sw.js rename to src/sw.ts index 0ef355d7..4aa1103f 100644 --- a/src/sw.js +++ b/src/sw.ts @@ -1,6 +1,11 @@ -const version = "v0.0.3"; +/// +// Empty export required due to --isolatedModules flag in tsconfig.json +export type {}; +declare const self: ServiceWorkerGlobalScope; +declare const clients: Clients; + const assets = ["simulator.html", "build/simulator.js", "build/firmware.js"]; -const cacheName = `simulator-${version}`; +const cacheName = `simulator-${process.env.version}`; self.addEventListener("install", (event) => { console.log("Installing simulator service worker..."); diff --git a/tsconfig.json b/tsconfig.json index 0716a2c2..40888c08 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -1,7 +1,7 @@ { "compilerOptions": { "target": "es2019", - "lib": ["dom", "dom.iterable", "esnext"], + "lib": ["dom", "dom.iterable", "esnext", "WebWorker"], "allowJs": true, "skipLibCheck": true, "esModuleInterop": true, From 328948b7935aed2cd3f567ffb5961be31175cf2c Mon Sep 17 00:00:00 2001 From: Robert Knight Date: Tue, 30 Apr 2024 15:14:34 +0100 Subject: [PATCH 07/14] Improve sw versioning, try flags with stage --- src/Makefile | 4 ++-- src/environment.ts | 3 +++ src/flags.ts | 56 ++++++++++++++++++++++++++++++++++++++++++++++ src/simulator.ts | 5 ++++- src/sw.ts | 2 +- 5 files changed, 66 insertions(+), 4 deletions(-) create mode 100644 src/environment.ts create mode 100644 src/flags.ts diff --git a/src/Makefile b/src/Makefile index f2f7de17..ace1bc40 100644 --- a/src/Makefile +++ b/src/Makefile @@ -146,8 +146,8 @@ $(BUILD)/micropython.js: $(OBJ) jshal.js simulator-js $(Q)emcc $(LDFLAGS) -o $(BUILD)/firmware.js $(OBJ) $(JSFLAGS) simulator-js: - npx esbuild ./simulator.ts --bundle --outfile=$(BUILD)/simulator.js --loader:.svg=text - npx esbuild --define:process.env.version=$$(cat ../package.json | jq .version) ./sw.ts --bundle --outfile=$(BUILD)/sw.js + npx esbuild '--define:process.env.STAGE="$(STAGE)"' ./simulator.ts --bundle --outfile=$(BUILD)/simulator.js --loader:.svg=text + npx esbuild --define:process.env.VERSION=$$(npm pkg get version) ./sw.ts --bundle --outfile=$(BUILD)/sw.js include $(TOP)/py/mkrules.mk diff --git a/src/environment.ts b/src/environment.ts new file mode 100644 index 00000000..7d295ca7 --- /dev/null +++ b/src/environment.ts @@ -0,0 +1,3 @@ +export type Stage = "local" | "REVIEW" | "STAGING" | "PRODUCTION"; + +export const stage = (process.env.STAGE || "local") as Stage; diff --git a/src/flags.ts b/src/flags.ts new file mode 100644 index 00000000..2e0a517b --- /dev/null +++ b/src/flags.ts @@ -0,0 +1,56 @@ +import { Stage, stage as stageFromEnvironment } from "./environment"; + +/** + * A union of the flag names (alphabetical order). + */ +export type Flag = + /** + * Enables service worker registration. + * + * Registers the service worker and enables offline use. + */ + "sw"; + +interface FlagMetadata { + defaultOnStages: Stage[]; + name: Flag; +} + +const allFlags: FlagMetadata[] = [{ name: "sw", defaultOnStages: [] }]; + +type Flags = Record; + +const flagsForParams = (stage: Stage, params: URLSearchParams) => { + const enableFlags = new Set(params.getAll("flag")); + const allFlagsDefault = enableFlags.has("none") + ? false + : enableFlags.has("*") + ? true + : undefined; + return Object.fromEntries( + allFlags.map((f) => [ + f.name, + isEnabled(f, stage, allFlagsDefault, enableFlags.has(f.name)), + ]) + ) as Flags; +}; + +const isEnabled = ( + f: FlagMetadata, + stage: Stage, + allFlagsDefault: boolean | undefined, + thisFlagOn: boolean +): boolean => { + if (thisFlagOn) { + return true; + } + if (allFlagsDefault !== undefined) { + return allFlagsDefault; + } + return f.defaultOnStages.includes(stage); +}; + +export const flags: Flags = (() => { + const params = new URLSearchParams(window.location.search); + return flagsForParams(stageFromEnvironment, params); +})(); diff --git a/src/simulator.ts b/src/simulator.ts index 5d3ebf10..4ca5d7c5 100644 --- a/src/simulator.ts +++ b/src/simulator.ts @@ -7,6 +7,7 @@ import { createMessageListener, Notifications, } from "./board"; +import { flags } from "./flags"; declare global { interface Window { @@ -48,7 +49,9 @@ function initServiceWorker() { }); } -initServiceWorker(); +if (flags.sw) { + initServiceWorker(); +} const fs = new FileSystem(); const board = createBoard(new Notifications(window.parent), fs); window.addEventListener("message", createMessageListener(board)); diff --git a/src/sw.ts b/src/sw.ts index 4aa1103f..4bb882f8 100644 --- a/src/sw.ts +++ b/src/sw.ts @@ -5,7 +5,7 @@ declare const self: ServiceWorkerGlobalScope; declare const clients: Clients; const assets = ["simulator.html", "build/simulator.js", "build/firmware.js"]; -const cacheName = `simulator-${process.env.version}`; +const cacheName = `simulator-${process.env.VERSION}`; self.addEventListener("install", (event) => { console.log("Installing simulator service worker..."); From 5dbbe0cab85577bb98f60ae3420b92120b245b5d Mon Sep 17 00:00:00 2001 From: Robert Knight Date: Tue, 30 Apr 2024 16:20:01 +0100 Subject: [PATCH 08/14] Build fix regarding version --- src/Makefile | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Makefile b/src/Makefile index ace1bc40..a28026ea 100644 --- a/src/Makefile +++ b/src/Makefile @@ -147,7 +147,7 @@ $(BUILD)/micropython.js: $(OBJ) jshal.js simulator-js simulator-js: npx esbuild '--define:process.env.STAGE="$(STAGE)"' ./simulator.ts --bundle --outfile=$(BUILD)/simulator.js --loader:.svg=text - npx esbuild --define:process.env.VERSION=$$(npm pkg get version) ./sw.ts --bundle --outfile=$(BUILD)/sw.js + npx esbuild '--define:process.env.VERSION="$((cat ./package.json | grep '"version":' | sed 's/"version": //' | sed 's/,//'))"' ./sw.ts --bundle --outfile=./sw.js include $(TOP)/py/mkrules.mk From 1524cb88f6eb6b52bd1d03cd2f7b241b3423d08b Mon Sep 17 00:00:00 2001 From: Robert Knight Date: Tue, 30 Apr 2024 16:40:00 +0100 Subject: [PATCH 09/14] Fix version code again --- src/Makefile | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Makefile b/src/Makefile index a28026ea..8051c15a 100644 --- a/src/Makefile +++ b/src/Makefile @@ -147,7 +147,7 @@ $(BUILD)/micropython.js: $(OBJ) jshal.js simulator-js simulator-js: npx esbuild '--define:process.env.STAGE="$(STAGE)"' ./simulator.ts --bundle --outfile=$(BUILD)/simulator.js --loader:.svg=text - npx esbuild '--define:process.env.VERSION="$((cat ./package.json | grep '"version":' | sed 's/"version": //' | sed 's/,//'))"' ./sw.ts --bundle --outfile=./sw.js + npx esbuild --define:process.env.VERSION="$$(cat ../package.json | grep '"version":' | sed 's/"version": //' | sed 's/,//' | sed 's/ //g')" ./sw.ts --bundle --outfile=$(BUILD)/sw.js include $(TOP)/py/mkrules.mk From 1331423d2b0b1050e4b06908560d8571a12f65e6 Mon Sep 17 00:00:00 2001 From: Robert Knight Date: Fri, 3 May 2024 09:49:06 +0100 Subject: [PATCH 10/14] Nicer version parsing --- src/Makefile | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Makefile b/src/Makefile index 8051c15a..90a5effc 100644 --- a/src/Makefile +++ b/src/Makefile @@ -147,7 +147,7 @@ $(BUILD)/micropython.js: $(OBJ) jshal.js simulator-js simulator-js: npx esbuild '--define:process.env.STAGE="$(STAGE)"' ./simulator.ts --bundle --outfile=$(BUILD)/simulator.js --loader:.svg=text - npx esbuild --define:process.env.VERSION="$$(cat ../package.json | grep '"version":' | sed 's/"version": //' | sed 's/,//' | sed 's/ //g')" ./sw.ts --bundle --outfile=$(BUILD)/sw.js + npx esbuild --define:process.env.VERSION="$$(node -e 'process.stdout.write(`"` + require("../package.json").version + `"`)')" ./sw.ts --bundle --outfile=$(BUILD)/sw.js include $(TOP)/py/mkrules.mk From 2063919a3156d7363c8e8ffe0a6518b26558a3d8 Mon Sep 17 00:00:00 2001 From: Robert Knight Date: Tue, 28 May 2024 10:24:00 +0100 Subject: [PATCH 11/14] Add firmware.wasm to service worker assets list --- src/sw.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/sw.ts b/src/sw.ts index 4bb882f8..2be22b63 100644 --- a/src/sw.ts +++ b/src/sw.ts @@ -4,7 +4,7 @@ export type {}; declare const self: ServiceWorkerGlobalScope; declare const clients: Clients; -const assets = ["simulator.html", "build/simulator.js", "build/firmware.js"]; +const assets = ["simulator.html", "build/simulator.js", "build/firmware.js", "build/firmware.wasm"]; const cacheName = `simulator-${process.env.VERSION}`; self.addEventListener("install", (event) => { From 6134ebd9c25f4794ffc8bec46c963b745ad2458d Mon Sep 17 00:00:00 2001 From: Robert Knight <95928279+microbit-robert@users.noreply.github.com> Date: Tue, 28 May 2024 10:27:36 +0100 Subject: [PATCH 12/14] Additional check on cache name before deletion. Co-authored-by: Matt Hillsdon <44397098+microbit-matt-hillsdon@users.noreply.github.com> --- src/sw.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/sw.ts b/src/sw.ts index 2be22b63..2126d359 100644 --- a/src/sw.ts +++ b/src/sw.ts @@ -25,7 +25,7 @@ self.addEventListener("activate", (event) => { const names = await caches.keys(); await Promise.all( names.map((name) => { - if (name !== cacheName) { + if (/^simulator-/.test(name) && name !== cacheName) { return caches.delete(name); } }) From e35db801c8b7710302ad7623dd839107bd8016e2 Mon Sep 17 00:00:00 2001 From: Robert Knight Date: Tue, 28 May 2024 11:01:04 +0100 Subject: [PATCH 13/14] Add code to unregister service worker if flag is not used --- src/simulator.ts | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/src/simulator.ts b/src/simulator.ts index 4ca5d7c5..07a8c222 100644 --- a/src/simulator.ts +++ b/src/simulator.ts @@ -51,7 +51,17 @@ function initServiceWorker() { if (flags.sw) { initServiceWorker(); +} else { + navigator.serviceWorker.getRegistrations().then(registrations => { + if (registrations.length > 0) { + // We should only have one service worker to unregister. + registrations[0].unregister().then(() => { + window.location.reload(); + }) + } + }) } + const fs = new FileSystem(); const board = createBoard(new Notifications(window.parent), fs); window.addEventListener("message", createMessageListener(board)); From a1c8cff6d0c45f0ef0c750d4585fcd2432c7aac5 Mon Sep 17 00:00:00 2001 From: Robert Knight Date: Tue, 28 May 2024 13:16:27 +0100 Subject: [PATCH 14/14] Move navigator.serviceWorker check --- src/simulator.ts | 74 +++++++++++++++++++++++------------------------- 1 file changed, 35 insertions(+), 39 deletions(-) diff --git a/src/simulator.ts b/src/simulator.ts index 07a8c222..7b129130 100644 --- a/src/simulator.ts +++ b/src/simulator.ts @@ -18,48 +18,44 @@ declare global { function initServiceWorker() { window.addEventListener("load", () => { - if ("serviceWorker" in navigator) { - navigator.serviceWorker.register("sw.js").then( - (registration) => { - console.log("Simulator service worker registration successful"); - // Reload the page when a new service worker is installed. - registration.onupdatefound = function () { - const installingWorker = registration.installing; - if (installingWorker) { - installingWorker.onstatechange = function () { - if ( - installingWorker.state === "installed" && - navigator.serviceWorker.controller - ) { - window.location.reload(); - } - }; - } - }; - }, - (error) => { - console.error( - `Simulator service worker registration failed: ${error}` - ); - } - ); - } else { - console.error("Service workers are not supported."); - } + navigator.serviceWorker.register("sw.js").then( + (registration) => { + console.log("Simulator service worker registration successful"); + // Reload the page when a new service worker is installed. + registration.onupdatefound = function () { + const installingWorker = registration.installing; + if (installingWorker) { + installingWorker.onstatechange = function () { + if ( + installingWorker.state === "installed" && + navigator.serviceWorker.controller + ) { + window.location.reload(); + } + }; + } + }; + }, + (error) => { + console.error(`Simulator service worker registration failed: ${error}`); + } + ); }); } -if (flags.sw) { - initServiceWorker(); -} else { - navigator.serviceWorker.getRegistrations().then(registrations => { - if (registrations.length > 0) { - // We should only have one service worker to unregister. - registrations[0].unregister().then(() => { - window.location.reload(); - }) - } - }) +if ("serviceWorker" in navigator) { + if (flags.sw) { + initServiceWorker(); + } else { + navigator.serviceWorker.getRegistrations().then((registrations) => { + if (registrations.length > 0) { + // We should only have one service worker to unregister. + registrations[0].unregister().then(() => { + window.location.reload(); + }); + } + }); + } } const fs = new FileSystem();