-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsw.js
32 lines (26 loc) · 885 Bytes
/
sw.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
const CACHE_NAME = `my-sample-app-cache-v1`;
// Use the install event to pre-cache all initial resources.
self.addEventListener('install', event => {
event.waitUntil((async () => {
const cache = await caches.open(CACHE_NAME);
cache.addAll(['/']);
})());
});
self.addEventListener('fetch', event => {
event.respondWith((async () => {
const cache = await caches.open(CACHE_NAME);
try {
// Try to fetch the resource from the network.
const fetchResponse = await fetch(event.request);
// Save the resource in the cache.
cache.put(event.request, fetchResponse.clone());
// And return it.
return fetchResponse;
} catch (e) {
// Fetching didn't work get the resource from the cache.
const cachedResponse = await cache.match(event.request);
// And return it.
return cachedResponse;
}
})());
});