-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathserviceworker.js
49 lines (45 loc) · 1.81 KB
/
serviceworker.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
40
41
42
43
44
45
46
47
48
49
// v2
const assets = ["/", "styles.css", "app.js", "sw-register.js",
"https://fonts.gstatic.com/s/materialicons/v67/flUhRq6tzZclQEJ-Vdg-IuiaDsNcIhQ8tQ.woff2"];
self.addEventListener("install", event => {
event.waitUntil( // wait until all large files have been downloaded
caches.open("assets").then( cache => {
console.log('open assets')
cache.addAll(assets);
})
);
});
// State while revalidate strategy
self.addEventListener('fetch', event => {
event.respondWith(
caches.match(event.request)
.then( cachedResponse => {
// Even if the response is in the cache, we fetch it
// and update the cache for future usage
console.log('fetchPromise')
const fetchPromise = fetch(event.request).then(
networkResponse => {
caches.open("assets").then( cache => {
cache.put(event.request, networkResponse.clone());
return networkResponse;
});
});
// We use the currently cached version if it's there
return cachedResponse || fetchPromise; // cached or a network fetch
})
);
});
// self.addEventListener("fetch", event => {
// event.respondWith(
// caches.match(event.request) // searching in the cache
// .then( response => {
// if (response) {
// // The request is in the cache
// return response; // cache hit
// } else {
// // We need to go to the network
// return fetch(event.request); // cache miss // not promise fail
// }
// })
// );
// });