-
Notifications
You must be signed in to change notification settings - Fork 1
/
sw.js
53 lines (48 loc) · 1.49 KB
/
sw.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
50
51
52
53
'use strict';
this.addEventListener('install', event => {
event.waitUntil(
caches.open('v1').then(cache => {
return cache.addAll([
'/static/main.css',
'/static/index.js',
'/static/images/vanilla192x192.png',
'/static/images/vanilla256x256.png',
'/static/images/vanilla512x512.png',
'/static/images/vanillaFavicon.png',
'/templates/index.html'
]);
})
);
});
this.addEventListener('activate', function(event) {
const cacheWhitelist = ['v1'];
event.waitUntil(
caches.keys().then(function(keyList) {
return Promise.all(keyList.map(function(key) {
if (cacheWhitelist.indexOf(key) === -1) {
return caches.delete(key);
}
}));
})
);
});
this.addEventListener('fetch', (event) => {
event.respondWith(
caches.match(event.request).then((response) => {
return response || fetch(event.request).then((networkResponse) => {
if (
networkResponse.headers.get('Content-Type') !==
'text/event-stream'
) {
let networkResponseClone = networkResponse.clone();
caches.open('v1').then((cache) => {
cache.put(event.request, networkResponseClone);
});
}
return networkResponse;
}).catch(() => {
return caches. match('/templates/index.html');
})
})
);
});