From bf1593bfb447704dea69e154f2451224df5649bb Mon Sep 17 00:00:00 2001 From: Pieter Vander Vennet Date: Fri, 24 Feb 2023 17:47:30 +0100 Subject: [PATCH] Fix service worker --- package.json | 2 +- service-worker.ts | 29 ++++++++--------------------- 2 files changed, 9 insertions(+), 22 deletions(-) diff --git a/package.json b/package.json index b83a0964c..2abcd562b 100644 --- a/package.json +++ b/package.json @@ -33,7 +33,7 @@ "query:licenses": "ts-node scripts/generateLicenseInfo.ts --query", "generate:contributor-list": "ts-node scripts/generateContributors.ts", "generate:schemas": "ts2json-schema -p Models/ThemeConfig/Json/ -o Docs/Schemas/ -t tsconfig.json -R . -m \".*ConfigJson\" && ts-node scripts/fixSchemas.ts ", - "generate:service-worker": "tsc service-worker.ts && git_hash=$(git rev-parse HEAD) && sed -i'.bkp' \"s/GITHUB-COMMIT/$git_hash/\" service-worker.js && rm service-worker.js.bkp", + "generate:service-worker": "tsc service-worker.ts --outFile public/service-worker.js && git_hash=$(git rev-parse HEAD) && sed -i \"s/GITHUB-COMMIT/$git_hash/\" public/service-worker.js", "optimize-images": "cd assets/generated/ && find -name '*.png' -exec optipng '{}' \\; && echo 'PNGs are optimized'", "generate:stats": "ts-node scripts/GenerateSeries.ts", "reset:layeroverview": "echo {\\\"layers\\\":[], \\\"themes\\\":[]} > ./assets/generated/known_layers_and_themes.json && echo {\\\"layers\\\": []} > ./assets/generated/known_layers.json && rm -f ./assets/generated/layers/*.json && rm -f ./assets/generated/themes/*.json && npm run generate:layeroverview && ts-node scripts/generateLayerOverview.ts --force", diff --git a/service-worker.ts b/service-worker.ts index 946b51d89..21e4e3482 100644 --- a/service-worker.ts +++ b/service-worker.ts @@ -7,14 +7,6 @@ interface ServiceWorkerFetchEvent extends Event { async function install() { console.log("Installing service worker!") - // const cache = await caches.open(version); - // console.log("Manifest file", manifest) - // await cache.addAll(manifest); - /* await cache.add({ - cache: "force-cache", - url: "http://4.bp.blogspot.com/-_vTDmo_fSTw/T3YTV0AfGiI/AAAAAAAAAX4/Zjh2HaoU5Zo/s1600/beautiful%2Bkitten.jpg", - destination: "image", - })//*/ } addEventListener("install", (e) => (e).waitUntil(install())) @@ -22,13 +14,6 @@ addEventListener("activate", (e) => (e).waitUntil(activate())) async function activate() { console.log("Activating service worker") - /*self.registration.showNotification("SW started", { - actions: [{ - action: "OK", - title: "Some action" - }] - })*/ - caches .keys() .then((keys) => { @@ -38,8 +23,8 @@ async function activate() { .catch(console.error) } -const cacheFirst = (event) => { - event.respondWith( +const cacheFirst = async (event) => { + await event.respondWith( caches.match(event.request, { ignoreSearch: true }).then((cacheResponse) => { if (cacheResponse !== undefined) { console.log("Loaded from cache: ", event.request) @@ -56,7 +41,7 @@ const cacheFirst = (event) => { ) } -self.addEventListener("fetch", (e) => { +self.addEventListener("fetch", async (e) => { // Important: this lambda must run synchronously, as the browser will otherwise handle the request const event = e try { @@ -64,7 +49,9 @@ self.addEventListener("fetch", (e) => { const requestUrl = new URL(event.request.url) if (requestUrl.pathname.endsWith("service-worker-version")) { console.log("Sending version number...") - event.respondWith(new Response(JSON.stringify({ "service-worker-version": version }))) + await event.respondWith( + new Response(JSON.stringify({ "service-worker-version": version })) + ) return } const shouldBeCached = @@ -77,9 +64,9 @@ self.addEventListener("fetch", (e) => { // We return _without_ calling event.respondWith, which signals the browser that it'll have to handle it himself return } - cacheFirst(event) + await cacheFirst(event) } catch (e) { console.error("CRASH IN SW:", e) - event.respondWith(fetch(event.request.url)) + await event.respondWith(fetch(event.request.url)) } })