-
Notifications
You must be signed in to change notification settings - Fork 3
/
sw.js
49 lines (43 loc) · 1.2 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
// -----------------------------------------------
// SERVICE WORKER - SW
// Author: ToVinhKhang
// Portfolio: https://tovinhkhang.netlify.app/
// -----------------------------------------------
// Init
var VERSION = "2.2.0";
var CURRENT_CACHES = {prefetch: 'Covid-19 Tracker v'+VERSION};
var file = [
"./",
"./css/styles_main.css",
"./css/styles_responsive.css",
"./js/functions/reloadPage.js"
];
// Add File Prefecth
self.addEventListener("install", e=>{
e.waitUntil(
caches.open(CURRENT_CACHES.prefetch).then(cache =>{return cache.addAll(file);})
);
});
// Fetch file Prefetch
self.addEventListener("fetch", e=>{
e.respondWith(
caches.match(e.request).then(response => {return response || fetch(e.request);})
);
});
// Delete All Caches
self.addEventListener('activate', e=>{
self.clients.claim(); var expectedCacheNames = Object.keys(CURRENT_CACHES).map(function(key){return CURRENT_CACHES[key];});
e.waitUntil(
caches.keys().then(function(cacheNames) {
return Promise.all(
cacheNames.map(function(cacheName){
if (expectedCacheNames.indexOf(cacheName) === -1){
console.log('Deleted Cache: ', cacheName);
return caches.delete(cacheName);
}
})
);
})
);
});
// End