Bri-Tunes/public/sw.js

72 lines
2.0 KiB
JavaScript

const CACHE = 'bri-tunes-v8';
const STATIC_PRECACHE = [
'/',
'/static/css/app.css',
'/static/js/player.js',
'/static/js/lightbox.js',
'/static/js/navigate.js',
'/static/js/bg-grid.js',
'/static/js/player-viz.js',
'/static/js/boot-sequence.js',
'/static/vendor/fonts/Orbitron.woff2',
'/static/vendor/fonts/Inter.woff2',
'/static/vendor/three/three.module.min.js',
];
self.addEventListener('install', (e) => {
e.waitUntil(caches.open(CACHE).then((c) => c.addAll(STATIC_PRECACHE)));
self.skipWaiting();
});
self.addEventListener('activate', (e) => {
e.waitUntil(
caches.keys().then((keys) =>
Promise.all(keys.filter((k) => k !== CACHE).map((k) => caches.delete(k)))
)
);
self.clients.claim();
});
self.addEventListener('notificationclick', (e) => {
e.notification.close();
e.waitUntil(
self.clients.matchAll({ type: 'window', includeUncontrolled: true }).then((clients) => {
const focused = clients.find((c) => c.focused);
if (focused) return focused.focus();
const existing = clients[0];
if (existing) return existing.focus();
return self.clients.openWindow('/');
})
);
});
self.addEventListener('fetch', (e) => {
const { request } = e;
const url = new URL(request.url);
// Only handle GET requests — POST/PUT/DELETE must never be cached.
if (request.method !== 'GET') return;
// Never intercept audio streams or cross-origin requests.
if (url.pathname.startsWith('/stream/') || url.origin !== self.location.origin) return;
// Static assets: cache-first.
if (url.pathname.startsWith('/static/')) {
e.respondWith(
caches.match(request).then((cached) => cached || fetch(request))
);
return;
}
// HTML pages: network-first, fall back to cache.
e.respondWith(
fetch(request)
.then((res) => {
const clone = res.clone();
caches.open(CACHE).then((c) => c.put(request, clone));
return res;
})
.catch(() => caches.match(request))
);
});