baton/frontend/sw.js

61 lines
1.5 KiB
JavaScript
Raw Permalink Normal View History

2026-03-20 21:09:05 +02:00
'use strict';
const CACHE_NAME = 'baton-v1';
// App shell assets to precache
const APP_SHELL = [
'/',
'/index.html',
'/app.js',
'/style.css',
'/manifest.json',
'/icons/icon-192.png',
'/icons/icon-512.png',
];
// Install: precache app shell + skipWaiting so new SW activates immediately
self.addEventListener('install', (event) => {
self.skipWaiting();
event.waitUntil(
caches.open(CACHE_NAME).then((cache) => cache.addAll(APP_SHELL))
);
});
// Activate: delete stale caches + claim open clients (decision: skipWaiting + clients.claim)
self.addEventListener('activate', (event) => {
event.waitUntil(
caches
.keys()
.then((keys) =>
Promise.all(
keys.filter((k) => k !== CACHE_NAME).map((k) => caches.delete(k))
)
)
.then(() => self.clients.claim())
);
});
// Fetch: cache-first for app shell; API calls pass through to network
self.addEventListener('fetch', (event) => {
if (event.request.method !== 'GET') return;
const url = new URL(event.request.url);
// Never intercept API calls — they must always reach the server
if (url.pathname.startsWith('/api/')) return;
event.respondWith(
caches.match(event.request).then((cached) => {
if (cached) return cached;
return fetch(event.request).then((response) => {
if (response && response.status === 200) {
const clone = response.clone();
caches.open(CACHE_NAME).then((cache) => cache.put(event.request, clone));
}
return response;
});
})
);
});