Fix service worker

This commit is contained in:
Pieter Vander Vennet 2023-02-24 17:47:30 +01:00
parent 6180b5074a
commit bf1593bfb4
2 changed files with 9 additions and 22 deletions

View file

@ -33,7 +33,7 @@
"query:licenses": "ts-node scripts/generateLicenseInfo.ts --query", "query:licenses": "ts-node scripts/generateLicenseInfo.ts --query",
"generate:contributor-list": "ts-node scripts/generateContributors.ts", "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: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'", "optimize-images": "cd assets/generated/ && find -name '*.png' -exec optipng '{}' \\; && echo 'PNGs are optimized'",
"generate:stats": "ts-node scripts/GenerateSeries.ts", "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", "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",

View file

@ -7,14 +7,6 @@ interface ServiceWorkerFetchEvent extends Event {
async function install() { async function install() {
console.log("Installing service worker!") 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) => (<any>e).waitUntil(install())) addEventListener("install", (e) => (<any>e).waitUntil(install()))
@ -22,13 +14,6 @@ addEventListener("activate", (e) => (<any>e).waitUntil(activate()))
async function activate() { async function activate() {
console.log("Activating service worker") console.log("Activating service worker")
/*self.registration.showNotification("SW started", {
actions: [{
action: "OK",
title: "Some action"
}]
})*/
caches caches
.keys() .keys()
.then((keys) => { .then((keys) => {
@ -38,8 +23,8 @@ async function activate() {
.catch(console.error) .catch(console.error)
} }
const cacheFirst = (event) => { const cacheFirst = async (event) => {
event.respondWith( await event.respondWith(
caches.match(event.request, { ignoreSearch: true }).then((cacheResponse) => { caches.match(event.request, { ignoreSearch: true }).then((cacheResponse) => {
if (cacheResponse !== undefined) { if (cacheResponse !== undefined) {
console.log("Loaded from cache: ", event.request) 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 // Important: this lambda must run synchronously, as the browser will otherwise handle the request
const event = <ServiceWorkerFetchEvent>e const event = <ServiceWorkerFetchEvent>e
try { try {
@ -64,7 +49,9 @@ self.addEventListener("fetch", (e) => {
const requestUrl = new URL(event.request.url) const requestUrl = new URL(event.request.url)
if (requestUrl.pathname.endsWith("service-worker-version")) { if (requestUrl.pathname.endsWith("service-worker-version")) {
console.log("Sending version number...") 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 return
} }
const shouldBeCached = 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 // We return _without_ calling event.respondWith, which signals the browser that it'll have to handle it himself
return return
} }
cacheFirst(event) await cacheFirst(event)
} catch (e) { } catch (e) {
console.error("CRASH IN SW:", e) console.error("CRASH IN SW:", e)
event.respondWith(fetch(event.request.url)) await event.respondWith(fetch(event.request.url))
} }
}) })