|
| 1 | +//SRC https://dev.to/100lvlmaster/create-a-pwa-with-sveltekit-svelte-a36 |
| 2 | +import { build, files, version } from '$service-worker'; |
| 3 | + |
| 4 | +const worker = (self as unknown) as ServiceWorkerGlobalScope; |
| 5 | +const FILES = `cache${version}`; |
| 6 | + |
| 7 | +// `build` is an array of all the files generated by the bundler, |
| 8 | +// `files` is an array of everything in the `static` directory |
| 9 | +const to_cache = build.concat(files); |
| 10 | +const staticAssets = new Set(to_cache); |
| 11 | + |
| 12 | +worker.addEventListener('install', (event) => { |
| 13 | + event.waitUntil( |
| 14 | + caches |
| 15 | + .open(FILES) |
| 16 | + .then((cache) => cache.addAll(to_cache)) |
| 17 | + .then(() => { |
| 18 | + worker.skipWaiting(); |
| 19 | + }) |
| 20 | + ); |
| 21 | +}); |
| 22 | + |
| 23 | +worker.addEventListener('activate', (event) => { |
| 24 | + event.waitUntil( |
| 25 | + caches.keys().then(async (keys) => { |
| 26 | + // delete old caches |
| 27 | + for (const key of keys) { |
| 28 | + if (key !== FILES) await caches.delete(key); |
| 29 | + } |
| 30 | + |
| 31 | + worker.clients.claim(); |
| 32 | + }) |
| 33 | + ); |
| 34 | +}); |
| 35 | + |
| 36 | +/** |
| 37 | + * Fetch the asset from the network and store it in the cache. |
| 38 | + * Fall back to the cache if the user is offline. |
| 39 | + */ |
| 40 | +async function fetchAndCache(request: Request) { |
| 41 | + const cache = await caches.open(`offline${version}`); |
| 42 | + |
| 43 | + try { |
| 44 | + const response = await fetch(request); |
| 45 | + cache.put(request, response.clone()); |
| 46 | + return response; |
| 47 | + } catch (err) { |
| 48 | + const response = await cache.match(request); |
| 49 | + if (response) return response; |
| 50 | + |
| 51 | + throw err; |
| 52 | + } |
| 53 | +} |
| 54 | + |
| 55 | +worker.addEventListener('fetch', (event) => { |
| 56 | + if (event.request.method !== 'GET' || event.request.headers.has('range')) return; |
| 57 | + |
| 58 | + const url = new URL(event.request.url); |
| 59 | + |
| 60 | + // don't try to handle e.g. data: URIs |
| 61 | + const isHttp = url.protocol.startsWith('http'); |
| 62 | + const isDevServerRequest = |
| 63 | + url.hostname === self.location.hostname && url.port !== self.location.port; |
| 64 | + const isStaticAsset = url.host === self.location.host && staticAssets.has(url.pathname); |
| 65 | + const skipBecauseUncached = event.request.cache === 'only-if-cached' && !isStaticAsset; |
| 66 | + |
| 67 | + if (isHttp && !isDevServerRequest && !skipBecauseUncached) { |
| 68 | + event.respondWith( |
| 69 | + (async () => { |
| 70 | + // always serve static files and bundler-generated assets from cache. |
| 71 | + // if your application has other URLs with data that will never change, |
| 72 | + // set this variable to true for them and they will only be fetched once. |
| 73 | + const cachedAsset = isStaticAsset && (await caches.match(event.request)); |
| 74 | + |
| 75 | + return cachedAsset || fetchAndCache(event.request); |
| 76 | + })() |
| 77 | + ); |
| 78 | + } |
| 79 | +}); |
| 80 | + |
0 commit comments