Skip to content

Commit 86584f5

Browse files
committed
Caching, again
1 parent ec0e24e commit 86584f5

2 files changed

Lines changed: 14 additions & 22 deletions

File tree

web/index.html

Lines changed: 1 addition & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -13,30 +13,18 @@
1313

1414
<script>
1515
if ('serviceWorker' in navigator) {
16-
// nocache=1: clear all caches and re-register SW to force latest version
1716
if (new URLSearchParams(location.search).get('nocache') === '1') {
1817
caches.keys().then(keys => Promise.all(keys.map(k => caches.delete(k)))).then(() => {
1918
navigator.serviceWorker.getRegistrations().then(regs => {
2019
Promise.all(regs.map(r => r.unregister())).then(() => {
21-
// Strip nocache param and reload clean
2220
const u = new URL(location.href);
2321
u.searchParams.delete('nocache');
2422
location.replace(u.toString());
2523
});
2624
});
2725
});
2826
} else {
29-
navigator.serviceWorker.register('sw.js').then(reg => {
30-
reg.addEventListener('updatefound', () => {
31-
const nw = reg.installing;
32-
if (!nw) return;
33-
nw.addEventListener('statechange', () => {
34-
if (nw.state === 'activated' && navigator.serviceWorker.controller) {
35-
location.reload();
36-
}
37-
});
38-
});
39-
});
27+
navigator.serviceWorker.register('sw.js');
4028
}
4129
}
4230
</script>

web/sw.js

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,11 @@ const APP_SHELL = [
1313

1414
self.addEventListener('install', (event) => {
1515
event.waitUntil(
16-
caches.open(CACHE_NAME).then((cache) => cache.addAll(APP_SHELL))
16+
caches.open(CACHE_NAME).then((cache) =>
17+
Promise.all(APP_SHELL.map((url) =>
18+
fetch(url, { cache: 'reload' }).then((resp) => cache.put(url, resp))
19+
))
20+
)
1721
);
1822
self.skipWaiting();
1923
});
@@ -51,12 +55,6 @@ self.addEventListener('fetch', (event) => {
5155
return;
5256
}
5357

54-
// nocache=1 in URL: bypass all caches and fetch from network
55-
if (url.searchParams.get('nocache') === '1') {
56-
event.respondWith(fetch(event.request, { cache: 'no-store' }));
57-
return;
58-
}
59-
6058
// Skip demo.note — large and not essential for offline
6159
if (url.pathname.endsWith('demo.note')) return;
6260

@@ -74,8 +72,14 @@ self.addEventListener('fetch', (event) => {
7472
return;
7573
}
7674

77-
// Local assets: cache-first, fall back to network
75+
// Local assets: network-first, fall back to cache (ensures fresh deploys)
7876
event.respondWith(
79-
caches.match(event.request).then((cached) => cached || fetch(event.request))
77+
fetch(event.request)
78+
.then((response) => {
79+
const clone = response.clone();
80+
caches.open(CACHE_NAME).then((cache) => cache.put(event.request, clone));
81+
return response;
82+
})
83+
.catch(() => caches.match(event.request))
8084
);
8185
});

0 commit comments

Comments
 (0)