fix(pwa): switch service worker to network-first strategy
Cache-first was preventing updates from reaching users. Network-first always fetches fresh content and only falls back to cache when offline. Also bumps cache version to evict stale cache-first entries.
This commit is contained in:
@@ -1,8 +1,6 @@
|
|||||||
const CACHE = 'apoena-v1';
|
const CACHE = 'apoena-v2';
|
||||||
const PRECACHE = ['/', '/worker.js', '/manifest.json', '/icon.svg', '/icon-maskable.svg'];
|
|
||||||
|
|
||||||
self.addEventListener('install', e => {
|
self.addEventListener('install', e => {
|
||||||
e.waitUntil(caches.open(CACHE).then(c => c.addAll(PRECACHE)));
|
|
||||||
self.skipWaiting();
|
self.skipWaiting();
|
||||||
});
|
});
|
||||||
|
|
||||||
@@ -21,7 +19,14 @@ self.addEventListener('fetch', e => {
|
|||||||
// Pass through API calls
|
// Pass through API calls
|
||||||
if (url.pathname === '/extract-audio') return;
|
if (url.pathname === '/extract-audio') return;
|
||||||
|
|
||||||
|
// Network-first: always fetch fresh content, update cache, fall back offline
|
||||||
e.respondWith(
|
e.respondWith(
|
||||||
caches.match(e.request).then(cached => cached || fetch(e.request))
|
fetch(e.request)
|
||||||
|
.then(res => {
|
||||||
|
const clone = res.clone();
|
||||||
|
caches.open(CACHE).then(c => c.put(e.request, clone));
|
||||||
|
return res;
|
||||||
|
})
|
||||||
|
.catch(() => caches.match(e.request))
|
||||||
);
|
);
|
||||||
});
|
});
|
||||||
|
|||||||
Reference in New Issue
Block a user