/
service-worker.js
72 lines (60 loc) · 2.24 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
65
66
67
68
69
70
71
72
import { skipWaiting, clientsClaim } from 'workbox-core';
import { precacheAndRoute } from 'workbox-precaching';
import { registerRoute, NavigationRoute } from 'workbox-routing';
import { CacheFirst, NetworkFirst } from 'workbox-strategies';
skipWaiting();
clientsClaim();
registerRoute(
new RegExp(/\.(?:png|gif|jpg|svg|ico|webp)$/),
new CacheFirst({
cacheName: 'image-cache-vue',
}),
'GET'
);
//new NavigationRoute('/index.html');
// The precache routes for workbox-webpack-plugin
precacheAndRoute(self.__WB_MANIFEST);
// listen to the install event
self.addEventListener('install', event => console.log('SW installed', event));
const sendNotificationDoneMessage = (notification, action = 'clicked') =>
self.clients.matchAll().then(clients => {
// Send the event to the client(s)
clients.forEach(client => {
client.postMessage({
type: 'notification-' + action,
messageId: notification.tag,
});
});
});
// listen to a notification click
self.addEventListener('notificationclick', event => {
const notification = event.notification;
const url = notification.data.url; // get the url passed on app/List/PushReminder.jsx#L73
// get all bowser tabs controlled by this SW
const eventWaitUntilFullfilled = self.clients.matchAll().then(clients => {
// open the application on the client
// check if there is already a tab with the url that should be opened
let windowToFocus = false;
clients.forEach(windowClient => {
if (windowClient.url === url) {
windowClient.focus(); // focus if url match
windowToFocus = windowClient;
}
});
if (!windowToFocus) {
// if no window already open, open a new
self.clients
.openWindow(url)
.then(windowClient => (windowClient ? windowClient.focus() : null));
}
// close the notification
notification.close();
return sendNotificationDoneMessage(notification);
});
// waitUntil needs a promise as argument. If not, the client.focus() and self.clients.openWindow() will fail
event.waitUntil(eventWaitUntilFullfilled);
});
// listen to the notification close
self.addEventListener('notificationclose', event =>
event.waitUntil(sendNotificationDoneMessage(event.notification, 'closed'))
);