-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsw.js
More file actions
97 lines (88 loc) · 2.97 KB
/
Copy pathsw.js
File metadata and controls
97 lines (88 loc) · 2.97 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
/* Cat Ski service worker
*
* Strategy:
* - On install: precache the app shell + icons.
* - On fetch:
* - Navigation requests / HTML -> network-first, fall back to cache
* (so code changes propagate on the next online load without the
* user getting stuck on a stale index.html).
* - Static assets (images, manifest) -> cache-first, update in
* background after response is served.
* - Cross-origin requests (analytics) pass through directly.
* - On activate: delete old caches with different names.
*
* When shipping a breaking change, bump CACHE_NAME (e.g. v2 -> v3) to
* force all old caches to clear on next visit.
*/
const CACHE_NAME = 'cat-ski-v15';
const APP_SHELL = [
'/',
'/index.html',
'/readme.html',
'/manifest.json',
'/preview.png',
'/favicon.ico',
'/favicon-16.png',
'/favicon-32.png',
'/favicon-48.png',
'/apple-touch-icon.png',
'/icon-192.png',
'/icon-512.png',
'/fonts/press-start-2p.woff2',
'/fonts/vt323.woff2',
];
self.addEventListener('install', event => {
event.waitUntil(
caches.open(CACHE_NAME)
.then(cache => cache.addAll(APP_SHELL))
.then(() => self.skipWaiting()) // activate immediately on next load
);
});
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())
);
});
self.addEventListener('fetch', event => {
const req = event.request;
if (req.method !== 'GET') return;
const url = new URL(req.url);
// Let cross-origin (analytics) pass through; the browser caches those
// itself and we don't want to complicate the SW.
if (url.origin !== self.location.origin) return;
// HTML / navigation requests: network-first so deploys surface right
// away when the user is online. Fall back to cached index when offline.
const isNavigation = req.mode === 'navigate' ||
(req.headers.get('accept') || '').includes('text/html');
if (isNavigation) {
event.respondWith(
fetch(req)
.then(response => {
const copy = response.clone();
caches.open(CACHE_NAME).then(cache => cache.put(req, copy));
return response;
})
.catch(() => caches.match(req).then(r => r || caches.match('/')))
);
return;
}
// Everything else (images, manifest, fonts that slip through): cache-
// first with background refresh. Serving from cache makes the app feel
// instant; the background fetch keeps the cache warm.
event.respondWith(
caches.match(req).then(cached => {
const networkFetch = fetch(req).then(response => {
if (response && response.ok) {
const copy = response.clone();
caches.open(CACHE_NAME).then(cache => cache.put(req, copy));
}
return response;
}).catch(() => cached); // offline + nothing cached = reject
return cached || networkFetch;
})
);
});