|
| 1 | +import { timestamp, files, shell, routes } from '@sapper/service-worker'; |
| 2 | + |
| 3 | +const ASSETS = `cache${timestamp}`; |
| 4 | + |
| 5 | +// `shell` is an array of all the files generated by the bundler, |
| 6 | +// `files` is an array of everything in the `static` directory |
| 7 | +const to_cache = shell.concat(files); |
| 8 | +const cached = new Set(to_cache); |
| 9 | + |
| 10 | +self.addEventListener('install', (event) => { |
| 11 | + event.waitUntil( |
| 12 | + caches |
| 13 | + .open(ASSETS) |
| 14 | + .then((cache) => cache.addAll(to_cache)) |
| 15 | + .then(() => { |
| 16 | + self.skipWaiting(); |
| 17 | + }) |
| 18 | + ); |
| 19 | +}); |
| 20 | + |
| 21 | +self.addEventListener('activate', (event) => { |
| 22 | + event.waitUntil( |
| 23 | + caches.keys().then(async (keys) => { |
| 24 | + // delete old caches |
| 25 | + for (const key of keys) { |
| 26 | + if (key !== ASSETS) await caches.delete(key); |
| 27 | + } |
| 28 | + |
| 29 | + self.clients.claim(); |
| 30 | + }) |
| 31 | + ); |
| 32 | +}); |
| 33 | + |
| 34 | +self.addEventListener('fetch', (event) => { |
| 35 | + if (event.request.method !== 'GET' || event.request.headers.has('range')) |
| 36 | + return; |
| 37 | + |
| 38 | + const url = new URL(event.request.url); |
| 39 | + |
| 40 | + // don't try to handle e.g. data: URIs |
| 41 | + if (!url.protocol.startsWith('http')) return; |
| 42 | + |
| 43 | + // ignore dev server requests |
| 44 | + if ( |
| 45 | + url.hostname === self.location.hostname && |
| 46 | + url.port !== self.location.port |
| 47 | + ) |
| 48 | + return; |
| 49 | + |
| 50 | + // always serve static files and bundler-generated assets from cache |
| 51 | + if (url.host === self.location.host && cached.has(url.pathname)) { |
| 52 | + event.respondWith(caches.match(event.request)); |
| 53 | + return; |
| 54 | + } |
| 55 | + |
| 56 | + // for pages, you might want to serve a shell `service-worker-index.html` file, |
| 57 | + // which Sapper has generated for you. It's not right for every |
| 58 | + // app, but if it's right for yours then uncomment this section |
| 59 | + /* |
| 60 | + if (url.origin === self.origin && routes.find(route => route.pattern.test(url.pathname))) { |
| 61 | + event.respondWith(caches.match('/service-worker-index.html')); |
| 62 | + return; |
| 63 | + } |
| 64 | + */ |
| 65 | + |
| 66 | + if (event.request.cache === 'only-if-cached') return; |
| 67 | + |
| 68 | + // for everything else, try the network first, falling back to |
| 69 | + // cache if the user is offline. (If the pages never change, you |
| 70 | + // might prefer a cache-first approach to a network-first one.) |
| 71 | + event.respondWith( |
| 72 | + caches.open(`offline${timestamp}`).then(async (cache) => { |
| 73 | + try { |
| 74 | + const response = await fetch(event.request); |
| 75 | + cache.put(event.request, response.clone()); |
| 76 | + return response; |
| 77 | + } catch (err) { |
| 78 | + const response = await cache.match(event.request); |
| 79 | + if (response) return response; |
| 80 | + |
| 81 | + throw err; |
| 82 | + } |
| 83 | + }) |
| 84 | + ); |
| 85 | +}); |
0 commit comments