diff options
| author | Fuwn <[email protected]> | 2023-09-14 20:59:08 -0700 |
|---|---|---|
| committer | Fuwn <[email protected]> | 2023-09-14 20:59:08 -0700 |
| commit | 4426cf6d2edeee0b12d05c5964fa16b542e0612e (patch) | |
| tree | bd7c35568d57002f1c125abfb288b99d09198523 /src | |
| parent | format(updates): add types (diff) | |
| download | due.moe-4426cf6d2edeee0b12d05c5964fa16b542e0612e.tar.xz due.moe-4426cf6d2edeee0b12d05c5964fa16b542e0612e.zip | |
feat: pwa
Diffstat (limited to 'src')
| -rw-r--r-- | src/app.html | 1 | ||||
| -rw-r--r-- | src/service-worker.ts | 80 |
2 files changed, 81 insertions, 0 deletions
diff --git a/src/app.html b/src/app.html index fb37d49a..627beba8 100644 --- a/src/app.html +++ b/src/app.html @@ -22,6 +22,7 @@ <link rel="stylesheet" type="text/css" href="https://skybox.sh/css/risotto.css" /> <!-- <link rel="stylesheet" type="text/css" href="https://skybox.sh/css/custom.css"> --> <link rel="shortcut icon" type="image/x-icon" href="%sveltekit.assets%/favicon.ico" /> + <link rel="manifest" href="/manifest.json" /> %sveltekit.head% </head> diff --git a/src/service-worker.ts b/src/service-worker.ts new file mode 100644 index 00000000..4c15bb6a --- /dev/null +++ b/src/service-worker.ts @@ -0,0 +1,80 @@ +/// <reference lib="webworker" /> + +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(); + }) + ); +}); + +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); + } + + worker.clients.claim(); + }) + ); +}); + +/** + * Fetch the asset from the network and store it in the cache. + * Fall back to the cache if the user is offline. + */ +async function fetchAndCache(request: Request) { + const cache = await caches.open(`offline${version}`); + + try { + const response = await fetch(request); + cache.put(request, response.clone()); + return response; + } catch (err) { + const response = await cache.match(request); + if (response) return response; + + 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. + const cachedAsset = isStaticAsset && (await caches.match(event.request)); + + return cachedAsset || fetchAndCache(event.request); + })() + ); + } +}); |