Service worker should update more aggressively now

This commit is contained in:
Pieter Vander Vennet 2024-07-16 16:11:14 +02:00
parent 7d0816219f
commit 47e23f7d8d
2 changed files with 23 additions and 12 deletions

View file

@ -11,6 +11,8 @@ mkdir dist/assets 2> /dev/null
export NODE_OPTIONS="--max-old-space-size=16384" 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 # This script ends every line with '&&' to chain everything. A failure will thus stop the build
npm run download:editor-layer-index && npm run download:editor-layer-index &&
@ -32,6 +34,7 @@ fi
BRANCH=`git rev-parse --abbrev-ref HEAD` BRANCH=`git rev-parse --abbrev-ref HEAD`
echo "The branch name is $BRANCH" echo "The branch name is $BRANCH"
if [ $BRANCH = "master" ] || [ $BRANCH = "develop" ] if [ $BRANCH = "master" ] || [ $BRANCH = "develop" ]
then then
ASSET_URL="./" ASSET_URL="./"

View file

@ -1,4 +1,4 @@
const version = "0.0.8-GITHUB-COMMIT" const version = "0.0.0"
interface ServiceWorkerFetchEvent extends Event { interface ServiceWorkerFetchEvent extends Event {
request: RequestInfo & { url: string } request: RequestInfo & { url: string }
@ -23,20 +23,27 @@ async function activate() {
.catch(console.error) .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( 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.debug("Loaded from cache: ", event.request)
if(attemptUpdate){
fetchAndCache(event)
}
return cacheResponse return cacheResponse
} }
return fetch(event.request).then((networkResponse) => { return fetchAndCache(event)
return caches.open(version).then((cache) => {
cache.put(event.request, networkResponse.clone())
console.log("Cached", event.request)
return networkResponse
})
})
}) })
) )
} }
@ -59,9 +66,10 @@ self.addEventListener("fetch", async (e) => {
origin.hostname !== "127.0.0.1" && origin.hostname !== "127.0.0.1" &&
origin.hostname !== "localhost" && origin.hostname !== "localhost" &&
!origin.hostname.endsWith(".local") && !origin.hostname.endsWith(".local") &&
!origin.host.endsWith(".gitpod.io") !origin.host.endsWith(".gitpod.io") &&
origin.pathname.indexOf("service-worker") < 0
if (!shouldBeCached) { 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 // We return _without_ calling event.respondWith, which signals the browser that it'll have to handle it himself
return return
} }