|
| 1 | +/// <reference types="@sveltejs/kit" /> |
| 2 | +/// <reference types="@sveltejs/kit" /> |
| 3 | +/// <reference no-default-lib="true"/> |
| 4 | +/// <reference lib="esnext" /> |
| 5 | +/// <reference lib="webworker" /> |
| 6 | + |
| 7 | +import { build, files, version } from '$service-worker' |
| 8 | + |
| 9 | +// Create a unique cache name for this deployment |
| 10 | +const CACHE = `cache-${version}` |
| 11 | + |
| 12 | +const ASSETS = [ |
| 13 | + ...build, // the app itself |
| 14 | + ...files, // everything in `static` |
| 15 | +] |
| 16 | + |
| 17 | +self.addEventListener('install', (event) => { |
| 18 | + // Create a new cache and add all files to it |
| 19 | + async function addFilesToCache() { |
| 20 | + const cache = await caches.open(CACHE) |
| 21 | + await cache.addAll(ASSETS) |
| 22 | + } |
| 23 | + |
| 24 | + event.waitUntil(addFilesToCache()) |
| 25 | +}) |
| 26 | + |
| 27 | +self.addEventListener('activate', (event) => { |
| 28 | + // Remove previous cached data from disk |
| 29 | + async function deleteOldCaches() { |
| 30 | + for (const key of await caches.keys()) { |
| 31 | + if (key !== CACHE) await caches.delete(key) |
| 32 | + } |
| 33 | + } |
| 34 | + |
| 35 | + event.waitUntil(deleteOldCaches()) |
| 36 | +}) |
| 37 | + |
| 38 | +self.addEventListener('fetch', (event) => { |
| 39 | + // ignore POST requests etc |
| 40 | + if (event.request.method !== 'GET') return |
| 41 | + |
| 42 | + async function respond() { |
| 43 | + const url = new URL(event.request.url) |
| 44 | + const cache = await caches.open(CACHE) |
| 45 | + |
| 46 | + // `build`/`files` can always be served from the cache |
| 47 | + if (ASSETS.includes(url.pathname)) { |
| 48 | + const response = await cache.match(url.pathname) |
| 49 | + |
| 50 | + if (response) { |
| 51 | + return response |
| 52 | + } |
| 53 | + } |
| 54 | + |
| 55 | + // for everything else, try the network first, but |
| 56 | + // fall back to the cache if we're offline |
| 57 | + try { |
| 58 | + const response = await fetch(event.request) |
| 59 | + |
| 60 | + // if we're offline, fetch can return a value that is not a Response |
| 61 | + // instead of throwing - and we can't pass this non-Response to respondWith |
| 62 | + if (!(response instanceof Response)) { |
| 63 | + throw new Error('invalid response from fetch') |
| 64 | + } |
| 65 | + |
| 66 | + if (response.status === 200) { |
| 67 | + cache.put(event.request, response.clone()) |
| 68 | + } |
| 69 | + |
| 70 | + return response |
| 71 | + } catch (err) { |
| 72 | + const response = await cache.match(event.request) |
| 73 | + |
| 74 | + if (response) { |
| 75 | + return response |
| 76 | + } |
| 77 | + |
| 78 | + // if there's no cache, then just error out |
| 79 | + // as there is nothing we can do to respond to this request |
| 80 | + throw err |
| 81 | + } |
| 82 | + } |
| 83 | + |
| 84 | + event.respondWith(respond()) |
| 85 | +}) |
0 commit comments