-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsw.js
62 lines (57 loc) · 1.79 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
const cacheName = "notification-v1.1.2--bootstrap-icons-v1.8.1";
self.addEventListener("install", (ev) => {
const cacheFiles = [
"./",
"./index.html",
"./scripts/index.js",
"./scripts/bootstrap-icons/icons.js",
"./scripts/bootstrap-icons/search.js",
"./styles/index.css",
"./favicon.ico",
"./icons/favicon-vector.svg",
"./icons/favicon-512.png",
"./icons/favicon-192.png",
];
ev.waitUntil(
(async () => {
const cache = await caches.open(cacheName);
await cache.addAll(cacheFiles);
})()
);
});
self.addEventListener("fetch", (ev) => {
ev.respondWith(
(async () => {
const resource = await caches.match(ev.request);
if (resource) return resource;
const response = await fetch(ev.request);
const cache = await caches.open(cacheName);
cache.put(ev.request, response.clone());
return response;
})()
);
});
self.addEventListener("activate", (ev) => {
ev.waitUntil(
caches.keys().then((keys) => {
return Promise.all(
keys.map((key) => {
if (key === cacheName) return;
return caches.delete(key);
})
);
})
);
});
self.addEventListener("notificationclick", (ev) => {
ev.notification.close();
let editUrl = `./?title=${encodeURIComponent(
ev.notification.title
)}&body=${encodeURIComponent(ev.notification.body)}`;
if (ev.notification.icon) {
const iconURL = ev.notification.icon;
const iconName = iconURL.split("/").pop().slice(0, -4);
editUrl += `&icon=${encodeURIComponent(iconName)}`;
}
self.clients.openWindow(editUrl);
});