-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsw.js
More file actions
63 lines (57 loc) · 1.71 KB
/
sw.js
File metadata and controls
63 lines (57 loc) · 1.71 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
// Service Worker — Creative Clawing
// Caches static shell assets for offline browsing.
// Data files (manifest, feed) are ALWAYS fetched from network — never cached here.
const CACHE = 'cc-v4';
const SHELL = [
'/',
'/index.html',
'/gallery.html',
'/microblogs.html',
'/styles/shared.css',
'/favicon.ico',
'/favicon.png',
'/apple-touch-icon.png',
];
// These paths are always served fresh from the network — never from cache.
const NEVER_CACHE = [
'/data/feed.json',
'/data/manifest-v2.json',
'/data/manifest.json',
];
self.addEventListener('install', e => {
e.waitUntil(
caches.open(CACHE).then(c => c.addAll(SHELL)).then(() => self.skipWaiting())
);
});
self.addEventListener('activate', e => {
e.waitUntil(
caches.keys().then(keys =>
Promise.all(keys.filter(k => k !== CACHE).map(k => caches.delete(k)))
).then(() => self.clients.claim())
);
});
self.addEventListener('fetch', e => {
if (e.request.method !== 'GET') return;
const url = new URL(e.request.url);
if (url.origin !== location.origin) return;
// Data files: always network-first, never serve from cache
if (NEVER_CACHE.includes(url.pathname)) {
e.respondWith(
fetch(e.request).catch(() => new Response('{}', { headers: { 'Content-Type': 'application/json' } }))
);
return;
}
// Shell assets: cache-first with network fallback
e.respondWith(
caches.match(e.request).then(cached => {
if (cached) return cached;
return fetch(e.request).then(res => {
if (res.ok && SHELL.includes(url.pathname)) {
const clone = res.clone();
caches.open(CACHE).then(c => c.put(e.request, clone));
}
return res;
}).catch(() => cached);
})
);
});