-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathservice-worker.js
39 lines (36 loc) · 1014 Bytes
/
service-worker.js
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
const OFFLINE_VERSION = 2;
const CACHE_NAME = 'offline';
self.addEventListener('install', (event) => {
event.waitUntil(
caches.open(CACHE_NAME).then((cache) => {
return cache.addAll([
'./',
'./index.html',
'./src/words.txt',
'./src/images/background.jpg',
]);
})
);
self.skipWaiting();
});
self.addEventListener('fetch', (event) => {
event.respondWith(
(async () => {
try {
// Always try the network first.
const networkResponse = await fetch(event.request);
return networkResponse;
} catch (error) {
// catch is only triggered if an exception is thrown, which is likely
// due to a network error.
console.log(
'Fetch failed; attempting to return cached asset.',
event.request.url
);
const cache = await caches.open(CACHE_NAME);
const cachedResponse = await cache.match(event.request);
return cachedResponse;
}
})()
);
});