-
Notifications
You must be signed in to change notification settings - Fork 0
/
service-worker.js
39 lines (38 loc) · 1.53 KB
/
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
// Fetch events, on registration of 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);
var fetchPromise = fetch(event.request).then((networkResponse) => {
// 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(
// Name the *cache* in the caches.open()...
caches.open('cache').then((cache) => {
// Take a list of URLs, then fetch them from the server and add the response to the cache...
return cache.addAll([
'./index.html',
'./style/index.css',
'./js/index.js',
'./images/*'
]);
})
);
});
// 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!");
});