-
Notifications
You must be signed in to change notification settings - Fork 0
/
service-worker
51 lines (50 loc) · 1.57 KB
/
service-worker
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
// Basic service worker...
self.addEventListener("fetch", (event) => {
event.respondWith(
caches.open("cache").then((cache) => {
return cache.match(event.request).then((response) => {
console.log("cache request: " + event.request.url);
const fetchPromise = fetch(event.request).then(
(networkResponse) => {
// If we got a response from the cache, update the cache...
console.log(
"fetch completed: " + event.request.url,
networkResponse
);
if (networkResponse) {
console.debug(
"updated cached page: " + event.request.url,
networkResponse
);
cache.put(event.request, networkResponse.clone());
}
return networkResponse;
},
(event) => {
// Rejected promise - just ignore it, we're offline...
console.log("Error in fetch()", event);
event.waitUntil(
// Our 'cache' here is named *cache* in the caches.open()...
caches.open("cache").then((cache) => {
return cache.addAll([
// List : cache.addAll(), //takes a list of URLs, then fetches them from...
// The server and adds the response to the cache...
"./index.php", // cache your index page
"./assets/*", // cache all images
"./app.webmanifest",
]);
})
);
}
);
// Respond from the cache, or the network...
return response || fetchPromise;
});
})
);
});
// Always updating i.e latest version available...
self.addEventListener("install", (event) => {
self.skipWaiting();
console.log("Latest version installed!");
});