|
| 1 | +import * as sapper from '@sapper/service-worker'; |
| 2 | + |
| 3 | +const ASSETS = `cache${sapper.timestamp}`; |
| 4 | + |
| 5 | +// `shell` is an array of all the files generated by webpack, |
| 6 | +// `files` is an array of everything in the `static` directory |
| 7 | +const to_cache = sapper.shell.concat(sapper.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') return; |
| 36 | + |
| 37 | + const url = new URL(event.request.url); |
| 38 | + |
| 39 | + // don't try to handle e.g. data: URIs |
| 40 | + if (!url.protocol.startsWith('http')) return; |
| 41 | + |
| 42 | + // ignore dev server requests |
| 43 | + if (url.hostname === self.location.hostname && url.port !== self.location.port) return; |
| 44 | + |
| 45 | + // always serve assets and webpack-generated files from cache |
| 46 | + if (url.host === self.location.host && cached.has(url.pathname)) { |
| 47 | + event.respondWith(caches.match(event.request)); |
| 48 | + return; |
| 49 | + } |
| 50 | + |
| 51 | + // for pages, you might want to serve a shell `index.html` file, |
| 52 | + // which Sapper has generated for you. It's not right for every |
| 53 | + // app, but if it's right for yours then uncomment this section |
| 54 | + /* |
| 55 | + if (url.origin === self.origin && routes.find(route => route.pattern.test(url.pathname))) { |
| 56 | + event.respondWith(caches.match('/index.html')); |
| 57 | + return; |
| 58 | + } |
| 59 | + */ |
| 60 | + |
| 61 | + if (event.request.cache === 'only-if-cached') return; |
| 62 | + |
| 63 | + // for everything else, try the network first, falling back to |
| 64 | + // cache if the user is offline. (If the pages never change, you |
| 65 | + // might prefer a cache-first approach to a network-first one.) |
| 66 | + event.respondWith( |
| 67 | + caches |
| 68 | + .open(`offline${sapper.timestamp}`) |
| 69 | + .then(async cache => { |
| 70 | + try { |
| 71 | + const response = await fetch(event.request); |
| 72 | + cache.put(event.request, response.clone()); |
| 73 | + return response; |
| 74 | + } catch(err) { |
| 75 | + const response = await cache.match(event.request); |
| 76 | + if (response) return response; |
| 77 | + |
| 78 | + throw err; |
| 79 | + } |
| 80 | + }) |
| 81 | + ); |
| 82 | +}); |
0 commit comments