generated from midzer/feed-dachau
-
Notifications
You must be signed in to change notification settings - Fork 0
/
sw.js
64 lines (59 loc) · 1.58 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
63
64
const version = '1.7.7';
const cacheName = `feed-wittichenau-${version}`;
self.addEventListener('install', function(event) {
event.waitUntil(
caches.open(cacheName).then(function(cache) {
return cache.addAll(
[
'/index.html',
'/assets/css/main.css',
'/assets/js/app.js'
]
);
})
);
});
self.addEventListener('fetch', function(event) {
if (event.request.method !== "POST") {
event.respondWith(
caches.open(cacheName).then(function(cache) {
return fetch(event.request).then(function(response) {
cache.put(event.request, response.clone());
return response;
});
})
);
}
});
self.addEventListener('activate', function(event) {
event.waitUntil(
caches.keys().then(function(cacheNames) {
return Promise.all(
cacheNames.filter(function(cacheName) {
// Return true if you want to remove this cache,
// but remember that caches are shared across
// the whole origin
}).map(function(cacheName) {
return caches.delete(cacheName);
})
);
})
);
});
self.addEventListener('push', event => {
const data = event.data.json();
self.registration.showNotification(data.title, {
body: data.body,
icon: '../../android-chrome-192x192.png',
data: data.link
});
if (navigator.setAppBadge) {
navigator.setAppBadge();
}
});
self.addEventListener('notificationclick', event => {
event.notification.close();
if (event.action !== 'close') {
clients.openWindow(event.notification.data);
}
});