Skip to content

Commit 551368c

Browse files
committed
Fix serviceworker to not so aggressively serve cached content
1 parent 98d851c commit 551368c

1 file changed

Lines changed: 13 additions & 21 deletions

File tree

sw.js

Lines changed: 13 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
1-
// This is the service worker with the combined offline experience (Offline page + Offline copy of pages)
1+
// Service worker for offline fallback only (network-first)
22

3-
const CACHE = "pwabuilder-offline-page";
4-
5-
importScripts('https://storage.googleapis.com/workbox-cdn/releases/5.1.2/workbox-sw.js');
3+
const CACHE = "pwabuilder-offline-page-v2";
64

75
const offlineFallbackPage = "offline.html";
86

@@ -19,31 +17,25 @@ self.addEventListener('install', async (event) => {
1917
);
2018
});
2119

22-
if (workbox.navigationPreload.isSupported()) {
23-
workbox.navigationPreload.enable();
24-
}
25-
26-
workbox.routing.registerRoute(
27-
new RegExp('/*'),
28-
new workbox.strategies.StaleWhileRevalidate({
29-
cacheName: CACHE
30-
})
31-
);
20+
self.addEventListener('activate', (event) => {
21+
event.waitUntil(
22+
caches.keys().then((cacheNames) =>
23+
Promise.all(
24+
cacheNames
25+
.filter((name) => name !== CACHE)
26+
.map((name) => caches.delete(name))
27+
)
28+
)
29+
);
30+
});
3231

3332
self.addEventListener('fetch', (event) => {
3433
if (event.request.mode === 'navigate') {
3534
event.respondWith((async () => {
3635
try {
37-
const preloadResp = await event.preloadResponse;
38-
39-
if (preloadResp) {
40-
return preloadResp;
41-
}
42-
4336
const networkResp = await fetch(event.request);
4437
return networkResp;
4538
} catch (error) {
46-
4739
const cache = await caches.open(CACHE);
4840
const cachedResp = await cache.match(offlineFallbackPage);
4941
return cachedResp;

0 commit comments

Comments
 (0)