fix: switch SW to network-first strategy so PWA always gets fresh updates
Cache-first meant users were stuck on old versions until manual cache clear. Network-first fetches live, updates cache, falls back to cache when offline. Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
1 parent
f298661d72
commit
1b981591d4
1 file changed
+5
-5
@@ -1,4 +1,4 @@
|
|||||||
const CACHE = 'ns-v20';
|
const CACHE = 'ns-v21';
|
||||||
const PRECACHE = ['/', 'index.html', 'styles.css', 'app.js', 'sw.js', 'manifest.json', 'icon.svg'];
|
const PRECACHE = ['/', 'index.html', 'styles.css', 'app.js', 'sw.js', 'manifest.json', 'icon.svg'];
|
||||||
const SKIP_URLS = ['/rest/stream', '/rest/download', '/config.js'];
|
const SKIP_URLS = ['/rest/stream', '/rest/download', '/config.js'];
|
||||||
|
|
||||||
@@ -19,14 +19,14 @@ self.addEventListener('activate', e => {
|
|||||||
self.addEventListener('fetch', e => {
|
self.addEventListener('fetch', e => {
|
||||||
if (SKIP_URLS.some(u => e.request.url.includes(u))) return;
|
if (SKIP_URLS.some(u => e.request.url.includes(u))) return;
|
||||||
e.respondWith(
|
e.respondWith(
|
||||||
caches.match(e.request).then(cached =>
|
fetch(e.request)
|
||||||
cached || fetch(e.request).then(res => {
|
.then(res => {
|
||||||
if (res.status === 200 && e.request.method === 'GET') {
|
if (res.status === 200 && e.request.method === 'GET') {
|
||||||
const clone = res.clone();
|
const clone = res.clone();
|
||||||
caches.open(CACHE).then(c => c.put(e.request, clone));
|
caches.open(CACHE).then(c => c.put(e.request, clone));
|
||||||
}
|
}
|
||||||
return res;
|
return res;
|
||||||
}).catch(() => cached)
|
})
|
||||||
)
|
.catch(() => caches.match(e.request).then(r => r || Response.error()))
|
||||||
);
|
);
|
||||||
});
|
});
|
||||||
Reference in new issue
Block a user