-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserviceworker.js
More file actions
51 lines (48 loc) · 1.24 KB
/
serviceworker.js
File metadata and controls
51 lines (48 loc) · 1.24 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
const version = "3.0.0";
const staticCache = `${version}staticfiles`;
addEventListener("install", e => {
skipWaiting();
e.waitUntil(
caches.open(staticCache).then(cache => {
// split into nice to have that isn't returned
// return must have cache
return cache.addAll([
"offline.html",
"img/typewriter-favicon.png",
"https://fonts.googleapis.com/css?family=Source+Sans+Pro:400,700&display=swap",
"styles.css"
]);
})
); // end waitUntil
});
addEventListener("activate", e => {
e.waitUntil(
// clean old caches
caches.keys().then(cacheNames => {
return (
Promise.all(
cacheNames.map(name => {
if (name !== staticCache) {
return caches.delete(name);
}
})
)
// take control over any opened tabs
.then(() => clients.claim())
);
})
); // end waitUntil
});
addEventListener("fetch", e => {
const request = e.request;
e.respondWith(
// return cache
caches.match(request).then(resp => {
if (resp) {
return resp;
}
// return network resp
return fetch(request).catch(() => caches.match('offline.html'));
})
); // end respondWith
});