-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsw.js
More file actions
64 lines (57 loc) · 1.95 KB
/
Copy pathsw.js
File metadata and controls
64 lines (57 loc) · 1.95 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
// EMW 2026 Service Worker — offline support for event day
// Strategy: stale-while-revalidate (fast cache response + background update)
//
// CACHE BUSTING: When you change index.html, podcast.html, code-guide.html,
// favicon.svg, or any other cached file, bump the version below (e.g. v1 → v2).
// This forces browsers to
// re-download everything on next visit. Without the bump, returning users
// may see stale content until the background revalidate completes.
const CACHE_NAME = 'emw2026-v11';
const PRECACHE_URLS = [
'./',
'./index.html',
'./podcast.html',
'./code-guide.html',
'./favicon.svg'
];
// Pre-cache app shell on install
self.addEventListener('install', event => {
event.waitUntil(
caches.open(CACHE_NAME)
.then(cache => cache.addAll(PRECACHE_URLS))
.then(() => self.skipWaiting())
);
});
// Clean up old caches on activate
self.addEventListener('activate', event => {
event.waitUntil(
caches.keys()
.then(keys => Promise.all(
keys.filter(k => k !== CACHE_NAME).map(k => caches.delete(k))
))
.then(() => self.clients.claim())
);
});
// Stale-while-revalidate: respond from cache immediately,
// fetch fresh copy in background and update cache for next load
self.addEventListener('fetch', event => {
const { request } = event;
// Only handle same-origin GET requests (skip Google Fonts CDN, etc.)
if (request.method !== 'GET' || !request.url.startsWith(self.location.origin)) {
return;
}
event.respondWith(
caches.open(CACHE_NAME).then(cache =>
cache.match(request).then(cached => {
const fetchPromise = fetch(request).then(response => {
if (response.ok) {
cache.put(request, response.clone());
}
return response;
}).catch(() => cached); // network failure: fall back to cache
// Return cache immediately if available, otherwise wait for network
return cached || fetchPromise;
})
)
);
});