-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathdetails-sw.js
More file actions
82 lines (74 loc) · 2.24 KB
/
details-sw.js
File metadata and controls
82 lines (74 loc) · 2.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
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
/* eslint-env serviceworker */
const cacheName = 'v2025.4/ffconf/details';
self.addEventListener('activate', (e) => {
e.waitUntil(
caches.keys().then((cacheNames) => {
return Promise.all(
cacheNames
.filter((name) => name !== cacheName)
.map((cacheName) => caches.delete(cacheName))
);
})
);
});
self.addEventListener('install', (e) => {
// once the SW is installed, go ahead and fetch the resources
// to make this work offline
e.waitUntil(
caches.open(cacheName).then((cache) => {
return cache
.addAll([
'/details/',
'/details/speaking/',
'/covid/',
'/code-of-conduct/',
// css
'/css/base.css',
'/css/article.css',
'/css/mq.css',
'/css/details.css',
// images
'/images/leftlogic.svg',
'/images/external.svg',
'/images/logo.svg',
'/images/favicons/favicon-16.png',
// fonts
'/fonts/BasierSquare-Medium-custom.woff2',
'/fonts/BasierSquare-Medium-custom.woff',
'/fonts/BasierSquare-Regular-custom.woff2',
'/fonts/BasierSquare-Regular-custom.woff',
])
.then(() => self.skipWaiting());
})
);
});
// when the browser fetches a url, try the network first,
// then fall back to the cache if the network fails
self.addEventListener('fetch', (event) => {
event.respondWith(
fetch(event.request)
.then((response) => {
// if we got a valid response, clone it and update the cache
if (response && response.status === 200) {
const responseToCache = response.clone();
caches.open(cacheName).then((cache) => {
cache.put(event.request, responseToCache);
});
}
return response;
})
.catch(() => {
// network failed, try the cache
return caches.match(event.request).then((cachedResponse) => {
if (cachedResponse) {
return cachedResponse;
}
return new Response('<html><body>This page is not cached', {
status: 404,
statusText: 'Not Found',
headers: { 'Content-Type': 'text/html' },
});
});
})
);
});