2026-04-18 13:17:52 +00:00
|
|
|
// posimai-boki SW — stale-while-revalidate + skipWaiting
|
2026-04-19 06:48:36 +00:00
|
|
|
const CACHE = 'posimai-boki-v15';
|
2026-04-19 06:40:09 +00:00
|
|
|
const STATIC = ['/', '/index.html', '/manifest.json', '/logo.png', '/js/app.js', '/js/data/drills.js', '/js/data/categories.js'];
|
2026-04-18 13:17:52 +00:00
|
|
|
|
|
|
|
|
self.addEventListener('install', e => {
|
|
|
|
|
e.waitUntil(
|
|
|
|
|
caches.open(CACHE).then(c => c.addAll(STATIC.filter(u => {
|
|
|
|
|
try { new URL(u, self.location.origin); return true; } catch { return false; }
|
|
|
|
|
})))
|
|
|
|
|
);
|
|
|
|
|
self.skipWaiting();
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
self.addEventListener('activate', e => {
|
|
|
|
|
e.waitUntil(
|
|
|
|
|
caches.keys().then(keys =>
|
|
|
|
|
Promise.all(keys.filter(k => k !== CACHE).map(k => caches.delete(k)))
|
|
|
|
|
).then(() => self.clients.claim())
|
|
|
|
|
);
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
self.addEventListener('fetch', e => {
|
|
|
|
|
if (e.request.method !== 'GET') return;
|
|
|
|
|
if (!e.request.url.startsWith(self.location.origin)) return;
|
|
|
|
|
|
|
|
|
|
e.respondWith(
|
|
|
|
|
caches.open(CACHE).then(cache =>
|
|
|
|
|
cache.match(e.request).then(cached => {
|
|
|
|
|
const network = fetch(e.request).then(res => {
|
|
|
|
|
if (res.ok && res.type === 'basic') cache.put(e.request, res.clone());
|
|
|
|
|
return res;
|
|
|
|
|
}).catch(() => cached);
|
|
|
|
|
return cached || network;
|
|
|
|
|
})
|
|
|
|
|
)
|
|
|
|
|
);
|
|
|
|
|
});
|