forked from StellarSplit/StellarSplit
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsw.js
More file actions
56 lines (47 loc) · 1.34 KB
/
Copy pathsw.js
File metadata and controls
56 lines (47 loc) · 1.34 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
importScripts('https://storage.googleapis.com/workbox-cdn/releases/6.5.4/workbox-sw.js');
const { registerRoute } = workbox.routing;
const { NetworkFirst, CacheFirst, StaleWhileRevalidate } = workbox.strategies;
const { BackgroundSyncPlugin } = workbox.backgroundSync;
// Precache
workbox.precaching.precacheAndRoute(self.__WB_MANIFEST || []);
// Cache API calls for splits
registerRoute(
({ url }) => url.pathname.startsWith('/api/splits'),
new NetworkFirst({
cacheName: 'splits-cache',
})
);
// Cache static assets
registerRoute(
({ request }) => request.destination === 'style' ||
request.destination === 'script',
new StaleWhileRevalidate({
cacheName: 'static-resources'
})
);
// Background Sync Queue
const bgSyncPlugin = new BackgroundSyncPlugin('paymentsQueue', {
maxRetentionTime: 24 * 60
});
registerRoute(
({ url }) => url.pathname.startsWith('/api/payments'),
new NetworkFirst({
plugins: [bgSyncPlugin]
}),
'POST'
);
// Push Notification Listener
self.addEventListener('push', (event) => {
const data = event.data.json();
self.registration.showNotification(data.title, {
body: data.body,
icon: '/icons/icon-192.png'
});
});
// Notification click
self.addEventListener('notificationclick', (event) => {
event.notification.close();
event.waitUntil(
clients.openWindow('/')
);
});