-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathsw.js
More file actions
99 lines (90 loc) · 2.88 KB
/
Copy pathsw.js
File metadata and controls
99 lines (90 loc) · 2.88 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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
const CACHE_NAME = 'celnav-v20';
const FONT_CACHE = 'celnav-fonts-v1';
const SHELL_ASSETS = [
'./',
'index.html',
'almanac.html',
'manifest.json',
'icon.svg',
'icon-180.png',
'icon-192.png',
'icon-512.png',
'coastline.js',
'https://unpkg.com/leaflet@1.9.4/dist/leaflet.css',
'https://unpkg.com/leaflet@1.9.4/dist/leaflet.js',
'https://fonts.googleapis.com/css2?family=Cinzel:wght@400;600&family=Share+Tech+Mono&family=Crimson+Pro:ital,wght@0,300;0,400;1,300&display=swap',
'https://fonts.googleapis.com/css2?family=STIX+Two+Text:ital,wght@0,400;0,700;1,400&family=Share+Tech+Mono&display=swap',
];
self.addEventListener('install', e => {
e.waitUntil(
caches.open(CACHE_NAME).then(cache => cache.addAll(SHELL_ASSETS))
);
self.skipWaiting();
});
self.addEventListener('activate', e => {
e.waitUntil(
caches.keys().then(keys =>
Promise.all(
keys
.filter(k => k !== CACHE_NAME && k !== FONT_CACHE)
.map(k => caches.delete(k))
)
)
);
self.clients.claim();
});
self.addEventListener('fetch', e => {
const url = new URL(e.request.url);
// Skip non-GET requests
if (e.request.method !== 'GET') return;
// Skip chrome-extension and /api/ requests
if (url.protocol === 'chrome-extension:') return;
if (url.pathname.startsWith('/api/')) return;
// Google Fonts (fonts.googleapis.com and fonts.gstatic.com) — cache-first, they're immutable
if (url.hostname === 'fonts.googleapis.com' || url.hostname === 'fonts.gstatic.com') {
e.respondWith(
caches.open(FONT_CACHE).then(cache =>
cache.match(e.request).then(cached => {
if (cached) return cached;
return fetch(e.request).then(response => {
if (response && response.status === 200) {
cache.put(e.request, response.clone());
}
return response;
});
})
)
);
return;
}
// Same-origin requests — network-first, fall back to cache, then cached "/"
if (url.origin === self.location.origin) {
e.respondWith(
fetch(e.request)
.then(response => {
if (response && response.status === 200) {
const clone = response.clone();
caches.open(CACHE_NAME).then(cache => cache.put(e.request, clone));
}
return response;
})
.catch(() =>
caches.match(e.request).then(cached => cached || caches.match('./'))
)
);
return;
}
// Other third-party (e.g. Leaflet CDN) — cache-first with network fallback
e.respondWith(
caches.match(e.request).then(cached => {
if (cached) return cached;
return fetch(e.request).then(response => {
if (response && response.status === 200) {
const clone = response.clone();
caches.open(CACHE_NAME).then(cache => cache.put(e.request, clone));
}
return response;
});
})
);
});