forked from vladmorosanu97/dev-v-dev
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathservice-worker.js
64 lines (55 loc) · 1.66 KB
/
service-worker.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;
const CACHE_NAME = `dev-v-dev-cache-${VERSION}`;
const RESOURCES_MANIFEST = 'resources-manifest.json';
self.addEventListener('install', event => {
event.waitUntil(
caches.open(CACHE_NAME).then(cache => {
return fetch(RESOURCES_MANIFEST)
.then(resp => resp.json())
.then(fileArr => cache.addAll(fileArr))
})
);
});
self.addEventListener('activate', function onActivate(event) {
event.waitUntil(
caches.keys().then(keys => {
keys.forEach(key => {
if (key !== CACHE_NAME) {
caches.delete(key);
}
});
})
);
});
self.addEventListener('fetch', event => {
if (event.request.url.indexOf(location.origin) === 0) {
const duplicatedRequest = event.request.clone();
event.respondWith(caches.match(event.request).then(resp => {
return resp || fetch(duplicatedRequest);
}));
}
});
self.addEventListener('push', event => {
event.waitUntil(
displayNotification(event.data.json())
);
});
function displayNotification(payload, tag = 'common-tag') {
const title = 'Dev v Dev';
const {
data
} = payload;
let resultText = "wins over";
if (data.result === 'dev2') {
resultText = "loses to";
} else if (data.result === 'draw') {
resultText = "draws"
}
return self.registration.showNotification(title, {
icon: 'src/icons/icon-512x512.png',
body: `${data.dev1} ${resultText} ${data.dev2}`,
tag,
vibrate: [100, 50, 100, 50, 100, 50],
requireInteraction: false
});
}