-
Notifications
You must be signed in to change notification settings - Fork 7
/
serviceWorker.js
93 lines (85 loc) · 2.49 KB
/
serviceWorker.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
const cacheName = 'v1';
const cacheAssets = [
'./index.html',
'./app.js',
'./manifest.json',
'./timetable.png'
];
//Call install event
self.addEventListener('install', (e) => {
//console.log('Service Worker: Installed');
e.waitUntil(
caches
.open(cacheName)
.then(cache => {
console.log('Service Worker: Caching Files');
cache.addAll(cacheAssets);
}
)
.then(() => self.skipWaiting())
);
});
//Call activate event
self.addEventListener('activate', (e) => {
//Remove unwanted caches
e.waitUntil(
caches.keys().then(cacheNames => {
return Promise.all(
cacheNames.map(cache => {
if(cache !== cacheName) {
console.log('Service Worker: Clearing Old Cache');
return caches.delete(cache);
}
})
)
})
);
console.log('Service Worker: Activated');
});
// self.addEventListener("fetch", (event) => {
// console.log("Fetching via Service worker");
// event.respondWith(
// // await fetch(event.request).then(res => {return res}).catch((e) => caches.match(event.request));
// caches.match(event.request).then(res => {return res}).catch((e) => console.log(e))
// );
// });
//Call fetch event
// self.addEventListener('fetch', (e) => {
// //console.log('Service Worker: Fetching');
// //e.respondWith(fetch(e.request).catch((error) => caches.match(e.request)));
// e.respondWith(
// (async () => {
// try {
// return await fetch(e.request);
// } catch (err) {
// // Try to get the response from a cache.
// const cachedResponse = await caches.match(e.request);
// // Return it if we found one.
// if (cachedResponse) return cachedResponse;
// // If we didn't find a match in the cache, use the network.
// }
// })(),
// );
// });
// addEventListener("fetch", (event) => {
// // Prevent the default, and handle the request ourselves.
// event.respondWith(
// (async () => {
// try{
// const res = await fetch(event.request);
// }
// catch(err){
// try{
// const cachedResponse = await caches.match(event.request.url);
// if (cachedResponse) return cachedResponse;
// }
// catch(err){
// console.log(err);
// }
// }
// })(),
// );
// });
// self.addEventListener('fetch', (e) => {
// console.log(e.request.url);
// });