48 lines
1.2 KiB
JavaScript
48 lines
1.2 KiB
JavaScript
// VIBN Service Worker — enables PWA install + basic offline shell
|
|
const CACHE = 'vibn-v1';
|
|
|
|
// Cache the app shell on install
|
|
self.addEventListener('install', (e) => {
|
|
e.waitUntil(
|
|
caches.open(CACHE).then(cache =>
|
|
cache.addAll(['/', '/manifest.json'])
|
|
)
|
|
);
|
|
self.skipWaiting();
|
|
});
|
|
|
|
self.addEventListener('activate', () => self.clients.claim());
|
|
|
|
// Network-first for API calls, cache-first for static assets
|
|
self.addEventListener('fetch', (e) => {
|
|
const { request } = e;
|
|
const url = new URL(request.url);
|
|
|
|
// Never cache API calls
|
|
if (url.pathname.startsWith('/api/')) return;
|
|
|
|
// Cache-first for static assets
|
|
if (
|
|
request.destination === 'image' ||
|
|
request.destination === 'font' ||
|
|
url.pathname.startsWith('/_next/static/')
|
|
) {
|
|
e.respondWith(
|
|
caches.match(request).then(cached => {
|
|
if (cached) return cached;
|
|
return fetch(request).then(res => {
|
|
const clone = res.clone();
|
|
caches.open(CACHE).then(c => c.put(request, clone));
|
|
return res;
|
|
});
|
|
})
|
|
);
|
|
return;
|
|
}
|
|
|
|
// Network-first for everything else (HTML pages)
|
|
e.respondWith(
|
|
fetch(request).catch(() => caches.match(request))
|
|
);
|
|
});
|