From 47e23f7d8d0009b274be33efb4b54335992c0da7 Mon Sep 17 00:00:00 2001 From: Pieter Vander Vennet Date: Tue, 16 Jul 2024 16:11:14 +0200 Subject: [PATCH] Service worker should update more aggressively now --- scripts/prepare-build.sh | 3 +++ src/service-worker.ts | 32 ++++++++++++++++++++------------ 2 files changed, 23 insertions(+), 12 deletions(-) diff --git a/scripts/prepare-build.sh b/scripts/prepare-build.sh index 61eeb9314..f8c9c1c12 100755 --- a/scripts/prepare-build.sh +++ b/scripts/prepare-build.sh @@ -11,6 +11,8 @@ mkdir dist/assets 2> /dev/null export NODE_OPTIONS="--max-old-space-size=16384" +VERSION=$(cat package.json | grep '"version": ' | sed "s/^.*://" | tr -d " \",") +sed "s/= \"0.0.0\"/= \"$VERSION\"/" -i public/service-worker.js # This script ends every line with '&&' to chain everything. A failure will thus stop the build npm run download:editor-layer-index && @@ -32,6 +34,7 @@ fi BRANCH=`git rev-parse --abbrev-ref HEAD` echo "The branch name is $BRANCH" + if [ $BRANCH = "master" ] || [ $BRANCH = "develop" ] then ASSET_URL="./" diff --git a/src/service-worker.ts b/src/service-worker.ts index 38cc9ec6c..45425bf60 100644 --- a/src/service-worker.ts +++ b/src/service-worker.ts @@ -1,4 +1,4 @@ -const version = "0.0.8-GITHUB-COMMIT" +const version = "0.0.0" interface ServiceWorkerFetchEvent extends Event { request: RequestInfo & { url: string } @@ -23,20 +23,27 @@ async function activate() { .catch(console.error) } -const cacheFirst = async (event) => { +function fetchAndCache(event){ + return fetch(event.request).then((networkResponse) => { + return caches.open(version).then((cache) => { + cache.put(event.request, networkResponse.clone()) + console.log("Cached", event.request) + return networkResponse + }) + }) +} + +const cacheFirst = async (event, attemptUpdate: boolean = false) => { await event.respondWith( caches.match(event.request, { ignoreSearch: true }).then((cacheResponse) => { if (cacheResponse !== undefined) { - console.log("Loaded from cache: ", event.request) + console.debug("Loaded from cache: ", event.request) + if(attemptUpdate){ + fetchAndCache(event) + } return cacheResponse } - return fetch(event.request).then((networkResponse) => { - return caches.open(version).then((cache) => { - cache.put(event.request, networkResponse.clone()) - console.log("Cached", event.request) - return networkResponse - }) - }) + return fetchAndCache(event) }) ) } @@ -59,9 +66,10 @@ self.addEventListener("fetch", async (e) => { origin.hostname !== "127.0.0.1" && origin.hostname !== "localhost" && !origin.hostname.endsWith(".local") && - !origin.host.endsWith(".gitpod.io") + !origin.host.endsWith(".gitpod.io") && + origin.pathname.indexOf("service-worker") < 0 if (!shouldBeCached) { - console.log("Not intercepting ", requestUrl.toString(), origin.host, requestUrl.host) + console.debug("Not intercepting ", requestUrl.toString(), origin.host, requestUrl.host) // We return _without_ calling event.respondWith, which signals the browser that it'll have to handle it himself return }