-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsw.js
More file actions
59 lines (54 loc) · 1.38 KB
/
sw.js
File metadata and controls
59 lines (54 loc) · 1.38 KB
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 = 'pomodoro-v9';
const ASSETS = [
'./',
'./index.html',
'./style.css',
'./app.js',
'./sync.js',
'./icon.svg',
'./manifest.json'
];
self.addEventListener('install', e => {
e.waitUntil(caches.open(CACHE_NAME).then(c => c.addAll(ASSETS)));
self.skipWaiting();
});
self.addEventListener('activate', e => {
e.waitUntil(
caches.keys().then(keys =>
Promise.all(keys.filter(k => k !== CACHE_NAME).map(k => caches.delete(k)))
)
);
self.clients.claim();
});
self.addEventListener('message', e => {
if (e.data && e.data.type === 'notification') {
self.registration.showNotification(e.data.title, {
body: e.data.body,
icon: 'icon.svg'
});
}
});
self.addEventListener('notificationclick', e => {
e.notification.close();
e.waitUntil(
self.clients.matchAll({ type: 'window', includeUncontrolled: true }).then(clients => {
if (clients.length) {
return clients[0].focus();
}
return self.clients.openWindow('./');
})
);
});
self.addEventListener('fetch', e => {
if (e.request.method !== 'GET') return;
if (!e.request.url.startsWith(self.location.origin)) return;
e.respondWith(
fetch(e.request)
.then(r => {
const clone = r.clone();
caches.open(CACHE_NAME).then(c => c.put(e.request, clone));
return r;
})
.catch(() => caches.match(e.request))
);
});