-
Notifications
You must be signed in to change notification settings - Fork 0
/
sw_page.js
74 lines (59 loc) · 1.55 KB
/
sw_page.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
const cacheName = "v1";
const cacheAssets = [
"css/styles.css",
"css/styles.scss",
"css/styles.css.map",
"app.js",
"main.js",
"index.html",
"logo.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 install event
self.addEventListener("activate", (e) => {
console.log(`Service Worker: Activated`);
// 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);
}
})
);
})
);
});
// call fetch event, this is where the offline func happens
self.addEventListener("fetch", (e) => {
console.log(`Service Worker: Fetching `);
// check if the cache storage is not empty or there is a request
e.respondWith(fetch(e.request).catch(() => caches.match(e.request)));
});
// service worker notification event
self.addEventListener('notificationclick', (event) => {
let notification = event.notification;
var action = event.action
console.log(notification);
if(action === 'confirm'){
console.log('Confirm was chosen');
notification.close();
} else{
console.log(action)
notification.close();
}
})