Files
mats ec02a6ddaf
Build and Publish / Metadata (push) Successful in 1s
Build and Publish / BuildAndDeployAmd64 (push) Successful in 24s
sw
2026-09-22 14:04:06 +02:00

37 lines
1.4 KiB
JavaScript

const CACHE = 'gibbz-shell-v1';
// Only stable, unhashed paths here — the built JS/CSS bundle gets a content
// hash in its filename (e.g. /assets/index-abc123.js), so it can't be listed
// up front. The fetch handler below caches it lazily on first real request.
const SHELL = ['/', '/index.html', '/manifest.webmanifest'];
self.addEventListener('install', (event) => {
event.waitUntil(caches.open(CACHE).then((cache) => cache.addAll(SHELL)));
self.skipWaiting();
});
self.addEventListener('activate', (event) => {
event.waitUntil(
caches.keys().then((keys) => Promise.all(keys.filter((k) => k !== CACHE).map((k) => caches.delete(k))))
);
self.clients.claim();
});
// Never touch the signaling socket or live API data — only cache the static
// app shell so the UI can still load (offline/flaky network), not room state.
self.addEventListener('fetch', (event) => {
const { request } = event;
const url = new URL(request.url);
if (request.method !== 'GET' || url.origin !== self.location.origin) return;
if (url.pathname.startsWith('/api/') || url.pathname === '/ws') return;
event.respondWith(
fetch(request)
.then((response) => {
const copy = response.clone();
caches.open(CACHE).then((cache) => cache.put(request, copy));
return response;
})
.catch(() => caches.match(request).then((cached) => cached || caches.match('/index.html')))
);
});