-
-
Notifications
You must be signed in to change notification settings - Fork 18
/
sw.js
70 lines (68 loc) · 1.7 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
65
66
67
68
69
70
const SW = require('../../src/sifrr.serviceworker');
const sw = new SW({
version: 1,
fallbackCacheName: 'ffff',
defaultCacheName: 'dddd',
policies: {
cachefirst: {
policy: 'CACHE_FIRST'
},
networkfirst: {
policy: 'NETWORK_FIRST',
cacheName: 'bangbang'
},
networkonly: {
policy: 'NETWORK_ONLY'
},
server: {
policy: 'NETWORK_ONLY'
},
cacheonly: {
policy: 'CACHE_ONLY',
cacheName: 'bangbang2'
},
cacheupdate: {
policy: 'CACHE_AND_UPDATE'
},
precache: {
policy: 'CACHE_ONLY',
cacheName: 'bangbang2'
}
},
fallbacks: {
status404: '/offline.html'
},
precacheUrls: ['/precache.js', '/cacheonly.js']
});
sw.setup();
sw.onInstall = () => {
self.skipWaiting();
};
sw.setupPushNotification('default title', { body: 'default body' });
sw.onNotificationClick = event => {
event.notification.close();
event.waitUntil(
self.clients
.matchAll({
type: 'window'
})
.then(function(clientList) {
for (let i = 0; i < clientList.length; i++) {
const client = clientList[i];
const url = new URL(client.url);
if (url.pathname == '/' && 'focus' in client) return client.focus();
}
if (self.clients.openWindow) return self.clients.openWindow('/');
})
);
};
self.addEventListener('message', async e => {
if (e.data === 'coverage') {
e.ports[0].postMessage(self.__coverage__);
} else if (e.data === 'caches') {
e.ports[0].postMessage(await caches.keys());
} else if (e.data.type && e.data.type === 'push') {
sw.pushEventListener(e.data.event).then(() => e.ports[0].postMessage('ok'));
}
});
module.exports = sw;