diff options
| author | Fuwn <[email protected]> | 2024-02-14 21:43:38 -0800 |
|---|---|---|
| committer | Fuwn <[email protected]> | 2024-02-14 21:43:38 -0800 |
| commit | 338f3577b614bf2c71870764f821888167616b50 (patch) | |
| tree | 5ca5be222f35e16177c19470f09dde007c912c13 /src/service-worker.ts | |
| parent | fix(days): subtract from episode if today (diff) | |
| download | due.moe-338f3577b614bf2c71870764f821888167616b50.tar.xz due.moe-338f3577b614bf2c71870764f821888167616b50.zip | |
refactor(pwa): use sveltekit's pwa example
Diffstat (limited to 'src/service-worker.ts')
| -rw-r--r-- | src/service-worker.ts | 129 |
1 files changed, 67 insertions, 62 deletions
diff --git a/src/service-worker.ts b/src/service-worker.ts index 91eada58..e5471acc 100644 --- a/src/service-worker.ts +++ b/src/service-worker.ts @@ -5,77 +5,82 @@ import { build, files, version } from '$service-worker'; -const worker = self as unknown as ServiceWorkerGlobalScope; -const FILES = `cache${version}`; -// `build` is an array of all the files generated by the bundler, -// `files` is an array of everything in the `static` directory -const to_cache = build.concat(files); -const staticAssets = new Set(to_cache); - -worker.addEventListener('install', (event) => { - event.waitUntil( - caches - .open(FILES) - .then((cache) => cache.addAll(to_cache)) - .then(() => { - worker.skipWaiting(); - }) - ); +const sw = /** @type {ServiceWorkerGlobalScope} */ /** @type {unknown} */ self; + +// 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()); }); -worker.addEventListener('activate', (event) => { - event.waitUntil( - caches.keys().then(async (keys) => { - // delete old caches - for (const key of keys) if (key !== FILES) await caches.delete(key); +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); + } + } - worker.clients.claim(); - }) - ); + event.waitUntil(deleteOldCaches()); }); -/** - * Fetch the asset from the network and store it in the cache. - * Fall back to the cache if the user is offline. - */ -const fetchAndCache = async (request: Request) => { - const cache = await caches.open(`offline${version}`); +self.addEventListener('fetch', (event) => { + // ignore POST requests etc + if (event.request.method !== 'GET') return; - try { - const response = await fetch(request); + async function respond() { + const url = new URL(event.request.url); + const cache = await caches.open(CACHE); - cache.put(request, response.clone()); + // `build`/`files` can always be served from the cache + if (ASSETS.includes(url.pathname)) { + const response = await cache.match(url.pathname); - return response; - } catch (err) { - const response = await cache.match(request); + if (response) { + return response; + } + } - 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); - throw err; + // 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; + } } -}; - -worker.addEventListener('fetch', (event) => { - if (event.request.method !== 'GET' || event.request.headers.has('range')) return; - - const url = new URL(event.request.url); - // don't try to handle e.g. data: URIs - const isHttp = url.protocol.startsWith('http'); - const isDevServerRequest = - url.hostname === self.location.hostname && url.port !== self.location.port; - const isStaticAsset = url.host === self.location.host && staticAssets.has(url.pathname); - const skipBecauseUncached = event.request.cache === 'only-if-cached' && !isStaticAsset; - - if (isHttp && !isDevServerRequest && !skipBecauseUncached) - event.respondWith( - (async () => { - // always serve static files and bundler-generated assets from cache. - // if your application has other URLs with data that will never change, - // set this variable to true for them and they will only be fetched once. - return ( - (isStaticAsset && (await caches.match(event.request))) || fetchAndCache(event.request) - ); - })() - ); + + event.respondWith(respond()); }); |