From 9ca55667b355b6e92954cc4c186f4b3376fad0b7 Mon Sep 17 00:00:00 2001 From: Chukwuma Nwaugha Date: Wed, 4 Dec 2024 11:08:10 +0000 Subject: [PATCH] use sveltekit approach to service worker setup --- app/src/app.html | 8 ---- app/src/service-worker.js | 80 +++++++++++++++++++++++++++++++++++++++ app/static/manifest.json | 3 +- app/static/sw.js | 40 -------------------- 4 files changed, 82 insertions(+), 49 deletions(-) create mode 100644 app/src/service-worker.js delete mode 100644 app/static/sw.js diff --git a/app/src/app.html b/app/src/app.html index 9c911b9..8c3a9e7 100644 --- a/app/src/app.html +++ b/app/src/app.html @@ -46,14 +46,6 @@
%sveltekit.body%
- - \ No newline at end of file diff --git a/app/src/service-worker.js b/app/src/service-worker.js new file mode 100644 index 0000000..0976a0c --- /dev/null +++ b/app/src/service-worker.js @@ -0,0 +1,80 @@ +/// +import { build, files, version } from '$service-worker'; + +// Create a unique cache name for this deployment +const CACHE = `cache-${version}`; + +const ASSETS = [ + ...build, // the app itself + ...files // everything in `static` +]; + +self.addEventListener('install', (event) => { + // Create a new cache and add all files to it + async function addFilesToCache() { + const cache = await caches.open(CACHE); + await cache.addAll(ASSETS); + } + + event.waitUntil(addFilesToCache()); +}); + +self.addEventListener('activate', (event) => { + // Remove previous cached data from disk + async function deleteOldCaches() { + for (const key of await caches.keys()) { + if (key !== CACHE) await caches.delete(key); + } + } + + event.waitUntil(deleteOldCaches()); +}); + +self.addEventListener('fetch', (event) => { + // ignore POST requests etc + if (event.request.method !== 'GET') return; + + async function respond() { + const url = new URL(event.request.url); + const cache = await caches.open(CACHE); + + // `build`/`files` can always be served from the cache + if (ASSETS.includes(url.pathname)) { + const response = await cache.match(url.pathname); + + if (response) { + return response; + } + } + + // for everything else, try the network first, but + // fall back to the cache if we're offline + try { + const response = await fetch(event.request); + + // if we're offline, fetch can return a value that is not a Response + // instead of throwing - and we can't pass this non-Response to respondWith + if (!(response instanceof Response)) { + throw new Error('invalid response from fetch'); + } + + if (response.status === 200) { + cache.put(event.request, response.clone()); + } + + return response; + } catch (err) { + const response = await cache.match(event.request); + + if (response) { + return response; + } + + // if there's no cache, then just error out + // as there is nothing we can do to respond to this request + throw err; + } + } + + event.respondWith(respond()); +}); diff --git a/app/static/manifest.json b/app/static/manifest.json index 3ed076c..dc13002 100644 --- a/app/static/manifest.json +++ b/app/static/manifest.json @@ -11,7 +11,8 @@ { "src": "/apple-touch-icon.png", "sizes": "152x152", - "type": "image/png" + "type": "image/png", + "purpose": "any maskable" } ], "screenshots": [ diff --git a/app/static/sw.js b/app/static/sw.js deleted file mode 100644 index dc989f4..0000000 --- a/app/static/sw.js +++ /dev/null @@ -1,40 +0,0 @@ -const CACHE_NAME = 'audiora-v1'; -const urlsToCache = ['/', '/manifest.json', '/apple-touch-icon.png']; - -self.addEventListener('install', (event) => { - event.waitUntil(caches.open(CACHE_NAME).then((cache) => cache.addAll(urlsToCache))); -}); - -self.addEventListener('activate', (event) => { - event.waitUntil( - caches.keys().then((cacheNames) => { - return Promise.all( - cacheNames.map((cacheName) => { - if (cacheName !== CACHE_NAME) { - return caches.delete(cacheName); - } - }) - ); - }) - ); -}); - -self.addEventListener('fetch', (event) => { - event.respondWith( - caches.match(event.request).then((response) => { - if (response) { - return response; - } - return fetch(event.request).then((response) => { - if (!response || response.status !== 200 || response.type !== 'basic') { - return response; - } - const responseToCache = response.clone(); - caches.open(CACHE_NAME).then((cache) => { - cache.put(event.request, responseToCache); - }); - return response; - }); - }) - ); -});