Improve service worker handling for DEV mode and cache updates

This commit is contained in:
2026-04-07 19:52:42 +02:00
parent 741b77aa2d
commit 74b54730cc
2 changed files with 31 additions and 3 deletions
+21 -1
View File
@@ -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 (
+10 -2
View File
@@ -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() {