Files
NovaSonic/sw.js
T
HoneyandClaude Sonnet 4.6 d9256e6294 fix: auto-reload all PWA windows when new SW activates
Chain activate: clear old caches → claim clients → navigate all windows.
client.navigate(url) forces a full reload so users always get the new
version without manually closing/reopening the PWA.
Also removed sw.js from PRECACHE (browser fetches SW directly, no need to cache it).

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
2026-04-25 18:10:08 +02:00

34 lines
1.1 KiB
JavaScript

const CACHE = 'ns-v23';
const PRECACHE = ['/', 'index.html', 'styles.css', 'app.js', 'manifest.json', 'icon.svg'];
const SKIP_URLS = ['/rest/stream', '/rest/download', '/config.js'];
self.addEventListener('install', e => {
e.waitUntil(caches.open(CACHE).then(c => c.addAll(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))))
.then(() => self.clients.claim())
.then(() => self.clients.matchAll({type: 'window', includeUncontrolled: true}))
.then(clients => clients.forEach(c => c.navigate(c.url)))
);
});
self.addEventListener('fetch', e => {
if (SKIP_URLS.some(u => e.request.url.includes(u))) return;
e.respondWith(
fetch(e.request)
.then(res => {
if (res.status === 200 && e.request.method === 'GET') {
const clone = res.clone();
caches.open(CACHE).then(c => c.put(e.request, clone));
}
return res;
})
.catch(() => caches.match(e.request).then(r => r || Response.error()))
);
});