-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathsw_cached_pages.js
46 lines (42 loc) · 1.4 KB
/
sw_cached_pages.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
var CACHE_NAME = 'mercury-cache';
//call install event
//attach event listener to the worker
self.addEventListener('install', e => {
console.log('Service worker installed');
});
//call activate event
self.addEventListener('activate', e => {
console.log('Service worker activated');
//clean up old/unwanted cache
e.waitUntil(
caches.keys()
.then(cacheNames => {
return Promise.all(
cacheNames.map(cache => {
if (cache !== CACHE_NAME) {
//clean old cache
return caches.delete(cache)
}
})
)
})
)
});
//call fetch event which loads cache files if offline
self.addEventListener('fetch', event => {
event.respondWith(
fetch(event.request)
.then(res => {
//This does not manually cache assets, but will cache all visited websites on-demand (since only a one pager)
//clone response
const resClone = res.clone();
//open cache
caches.open(CACHE_NAME)
.then(cache => {
//add response to cache
cache.put(event.request, resClone);
});
return res;
}).catch(err => caches.match(event.request).then(res => res))
);
});