-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathservice_worker.js
59 lines (56 loc) · 1.86 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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
const CACHE_NAME = "fight-corona-rajchandra-me";
const urlsToCache = [
"/index.html",
"/src/css/index.css",
"/src/css/mobile.css",
"/src/documents/prevention_measures/english_dos_donts.png",
"/src/documents/prevention_measures/hindi_dos_donts.png",
"/src/images/icons/",
"/src/js/common/local_storage.js",
"/src/js/requests/config.js",
"/src/js/requests/cookie.js",
"/src/js/requests/main.js",
"/src/js/ui/charts/line.js",
"/src/js/ui/translation/translate.js",
"/src/js/ui/translation/variables.js",
"/src/js/ui/country.js",
"/src/js/ui/news.js",
"/src/js/ui/global.js",
"/src/js/index.js"
]
self.addEventListener('install', function(event) {
// Perform install steps
event.waitUntil(
caches.open(CACHE_NAME)
.then(function(cache) {
console.log('Opened cache');
return cache.addAll(urlsToCache);
})
);
});
self.addEventListener('fetch', function(event) {
// console.log('INSIDE FETCH --->',event.request.url);
event.respondWith(
caches.match(event.request)
.then(function(response) {
if (response) {
return response; // if valid response is found in cache return it
} else {
return fetch(event.request) //fetch from internet
.then(function(res) {
return caches.open(CACHE_NAME)
.then(function(cache) {
cache.put(event.request.url, res.clone()); //save the response for future
return res; // return the fetched data
})
})
.catch(function(err) { // fallback mechanism
return caches.open(CACHE_NAME)
.then(function(cache) {
return cache.match('/index.html');
});
});
}
})
);
});