-
Notifications
You must be signed in to change notification settings - Fork 0
/
serviceworker.js
48 lines (44 loc) · 1.38 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
// 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(
caches.open("assets").then((cache) => {
cache.addAll(assets);
})
);
});
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
const fetchPromise = fetch(event.request).then((networkResponse) => {
caches.open("assets").then((cache) => {
cache.put(event.request, networkResponse.clone());
return networkResponse;
});
});
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
// }
// })
// );
// });