34 lines
1.2 KiB
JavaScript
34 lines
1.2 KiB
JavaScript
const CACHE = 'gibbz-shell-v1';
|
|
const SHELL = ['/', '/index.html', '/style.css', '/app.js', '/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')))
|
|
);
|
|
});
|