diff --git a/public/service-worker.js b/public/service-worker.js index 8630cfe..a62cb76 100644 --- a/public/service-worker.js +++ b/public/service-worker.js @@ -1,4 +1,4 @@ -const CACHE_NAME = 'lonc-shell-v1'; +const CACHE_NAME = 'lonc-shell-v2'; const APP_SHELL = ['/', '/index.html', '/manifest.webmanifest', '/offline.html', '/icons/icon.svg', '/icons/icon-mask.svg']; self.addEventListener('install', (event) => { @@ -40,6 +40,26 @@ self.addEventListener('fetch', (event) => { return; } + const destination = event.request.destination; + if ( + destination === 'script' || + destination === 'style' || + destination === 'worker' || + requestUrl.pathname.endsWith('.js') || + requestUrl.pathname.endsWith('.css') + ) { + event.respondWith( + fetch(event.request) + .then((networkResponse) => { + const clone = networkResponse.clone(); + caches.open(CACHE_NAME).then((cache) => cache.put(event.request, clone)); + return networkResponse; + }) + .catch(() => caches.match(event.request)), + ); + return; + } + event.respondWith( caches.match(event.request).then((response) => { return ( diff --git a/src/app/bootstrap.js b/src/app/bootstrap.js index 576792a..f58d23b 100644 --- a/src/app/bootstrap.js +++ b/src/app/bootstrap.js @@ -10,9 +10,17 @@ import { navBar } from '../components/nav-bar.js'; import { registerFeatureData } from '../features/register.js'; async function installServiceWorker() { - if ('serviceWorker' in navigator) { - await navigator.serviceWorker.register('/service-worker.js'); + if (!('serviceWorker' in navigator)) { + return; } + + if (import.meta.env.DEV) { + const registrations = await navigator.serviceWorker.getRegistrations(); + await Promise.all(registrations.map((registration) => registration.unregister())); + return; + } + + await navigator.serviceWorker.register('/service-worker.js'); } export function bootstrapApp() {