|
| 1 | +const CACHE_VERSION = 'v1'; |
| 2 | +const CACHE_NAME = `led-vocab-${CACHE_VERSION}`; |
| 3 | + |
| 4 | +const ASSETS_TO_CACHE = [ |
| 5 | + './', |
| 6 | + 'index.html', |
| 7 | + 'app.css', |
| 8 | + 'manifest.webmanifest', |
| 9 | + 'favicon.ico', |
| 10 | + '3rd-party/bootstrap/bootstrap.bundle.min.js', |
| 11 | + '3rd-party/bootstrap/bootstrap.bundle.min.js.map', |
| 12 | + '3rd-party/bootstrap/bootstrap.min.css', |
| 13 | + '3rd-party/bootstrap-icons/bootstrap-icons.css', |
| 14 | + '3rd-party/bootstrap-icons/bootstrap-icons.min.css', |
| 15 | + '3rd-party/bootstrap-icons/fonts/bootstrap-icons.woff', |
| 16 | + '3rd-party/bootstrap-icons/fonts/bootstrap-icons.woff2', |
| 17 | + '3rd-party/jquery/jquery-3.7.1.min.js', |
| 18 | + 'fonts/JF-Dot-jiskan16-1990.woff2', |
| 19 | + 'js/app.js', |
| 20 | + 'js/constants.js', |
| 21 | + 'js/data-sources.js', |
| 22 | + 'js/DisplayBuffer.js', |
| 23 | + 'js/DisplayBufferPair.js', |
| 24 | + 'js/effects.js', |
| 25 | + 'js/MarqueeText.js', |
| 26 | + 'js/ModuleController.js', |
| 27 | + 'js/PortMutex.js', |
| 28 | + 'js/RawPortOperations.js', |
| 29 | + 'js/util.js', |
| 30 | + 'datasets/jlpt-words-by-level.json', |
| 31 | + 'datasets/jawiki-2022-08-29.json', |
| 32 | + 'icons/icon-192.png', |
| 33 | + 'icons/icon-512.png', |
| 34 | +]; |
| 35 | + |
| 36 | +self.addEventListener('install', event => { |
| 37 | + console.log('[ServiceWorker] Installing...'); |
| 38 | + event.waitUntil( |
| 39 | + caches.open(CACHE_NAME).then(cache => { |
| 40 | + console.log('[ServiceWorker] Caching assets...'); |
| 41 | + return cache.addAll(ASSETS_TO_CACHE); |
| 42 | + }) |
| 43 | + ); |
| 44 | +}); |
| 45 | + |
| 46 | +self.addEventListener('activate', event => { |
| 47 | + console.log('[ServiceWorker] Activating...'); |
| 48 | + event.waitUntil( |
| 49 | + caches.keys().then(cacheNames => { |
| 50 | + return Promise.all( |
| 51 | + cacheNames.map(cacheName => { |
| 52 | + if (cacheName !== CACHE_NAME) { |
| 53 | + console.log('[ServiceWorker] Deleting old cache:', cacheName); |
| 54 | + return caches.delete(cacheName); |
| 55 | + } |
| 56 | + }) |
| 57 | + ); |
| 58 | + }) |
| 59 | + ); |
| 60 | + self.clients.claim(); |
| 61 | +}); |
| 62 | + |
| 63 | +self.addEventListener('fetch', event => { |
| 64 | + const isSameOrigin = new URL(event.request.url).origin === location.origin; |
| 65 | + const isGetRequest = event.request.method === 'GET'; |
| 66 | + |
| 67 | + // Assume cross-origin requests aren't cacheable (no CDNs!) |
| 68 | + if (!isGetRequest || !isSameOrigin) return; |
| 69 | + |
| 70 | + const req = event.request; |
| 71 | + |
| 72 | + // Avoid the gnarly `then` chain |
| 73 | + const res = (async () => { |
| 74 | + try { |
| 75 | + let response = await caches.match(req); |
| 76 | + if (!response) { |
| 77 | + response = await fetch(req); |
| 78 | + if (response && response.status == 200) { |
| 79 | + console.log('[ServiceWorker] Cache miss:', req.url); |
| 80 | + const copy = response.clone(); |
| 81 | + caches.open(CACHE_NAME).then(cache => cache.put(req, copy)); |
| 82 | + } |
| 83 | + } |
| 84 | + return response; |
| 85 | + } catch { |
| 86 | + console.log('[ServiceWorker] Fetch failed:', req.url); |
| 87 | + } |
| 88 | + })(); |
| 89 | + |
| 90 | + event.respondWith(res); |
| 91 | +}); |
0 commit comments