-
Notifications
You must be signed in to change notification settings - Fork 0
/
sw.js
99 lines (87 loc) · 2.57 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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
const cacheName = 'v1';
const cacheAssets = [
'/',
'/page/index.html',
'/style/style.css',
'/script/script.js',
'/images/',
'/images/default.jpg',
'/images/app_logo.png',
'/apple-touch-icon.png',
'/android-chrome-192x192.png',
'/maskable_icon.png',
'/favicon.ico',
'/safari-pinned-tab.svg',
'/sound/sound.mp3'
];
self.addEventListener('install', (event) => {
event.waitUntil(
caches
.open(cacheName)
.then( (cache) => {
return cache.addAll(cacheAssets);
})
);
});
self.addEventListener('activate', (e) => {
// clearing old cache
e.waitUntil(
caches.keys().then((cacheNames) => {
return Promise.all(
cacheNames.map((cache) => {
if (cache !== cacheName) {
console.log('Service Worker is clearing old cache');
return caches.delete(cache);
};
})
);
})
);
});
self.addEventListener('fetch', (event) => {
// console.log(event.request.url);
// event.respondWith(fetch(event.request).catch( () => caches.match(event.request))
event.respondWith(
caches.match(event.request).then( (resp) => {
return resp || fetch(event.request).then( (response) => {
let responseClone = response.clone();
caches.open('v1').then( (cache) => {
cache.put(event.request, responseClone);
return response;
});
return response;
});
}).catch( () => {
return caches.match('/images/default.jpg');
})
);
});
// To update
// self.addEventListener('install', (event) => {
// event.waitUntil(
// caches.open('v2').then((cache) => {
// return cache.addAll([
// './sw-test/',
// './sw-test/index.html',
// './sw-test/style.css',
// './sw-test/app.js',
// './sw-test/image-list.js',
// ...
// // include other new resources for the new version...
// ]);
// })
// );
// });
//Deleting old caches
// self.addEventListener('activate', (event) => {
// var cacheKeeplist = ['v2'];
// event.waitUntil(
// caches.keys().then((keyList) => {
// return Promise.all(keyList.map((key) => {
// if (cacheKeeplist.indexOf(key) === -1) {
// return caches.delete(key);
// }
// }));
// })
// );
// });