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
103 lines (90 loc) · 3.05 KB
/
Copy pathsw.js
File metadata and controls
103 lines (90 loc) · 3.05 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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
importScripts('https://storage.googleapis.com/workbox-cdn/releases/6.5.4/workbox-sw.js');
const { registerRoute, setCatchHandler } = workbox.routing;
const { NetworkFirst, CacheFirst, StaleWhileRevalidate } = workbox.strategies;
const { BackgroundSyncPlugin } = workbox.backgroundSync;
const { ExpirationPlugin } = workbox.expiration;
const CACHE_NAME = 'stellarsplit-v1';
const STATIC_CACHE = `${CACHE_NAME}-static`;
const OFFLINE_URL = '/offline.html';
// Precache the app shell + offline fallback. self.__WB_MANIFEST is only
// populated when this file is processed by a build step (injectManifest);
// the explicit fallback list below covers dev / plain-static serving too.
workbox.precaching.precacheAndRoute([
...(self.__WB_MANIFEST || []),
{ url: '/', revision: CACHE_NAME },
{ url: '/index.html', revision: CACHE_NAME },
{ url: '/manifest.json', revision: CACHE_NAME },
{ url: OFFLINE_URL, revision: CACHE_NAME },
]);
// Cache-first for static assets: JS/CSS chunks (including lazy-loaded
// route chunks like analytics/dashboard) and images.
registerRoute(
({ request, url }) =>
request.destination === 'script' ||
request.destination === 'style' ||
request.destination === 'image' ||
/\.(js|css|png)$/.test(url.pathname),
new CacheFirst({
cacheName: STATIC_CACHE,
plugins: [
new ExpirationPlugin({ maxEntries: 100, maxAgeSeconds: 30 * 24 * 60 * 60 }),
],
})
);
// Network-first for API calls, falling back to cache when offline.
registerRoute(
({ url }) => url.pathname.startsWith('/api/splits'),
new NetworkFirst({
cacheName: 'splits-cache',
})
);
// Network-first for navigations, falling back to cached page, then to the
// offline page if nothing cached matches (e.g. first-ever offline visit
// to a lazy-loaded route like /analytics or /dashboard).
registerRoute(
({ request }) => request.mode === 'navigate',
new NetworkFirst({
cacheName: STATIC_CACHE,
})
);
// Lets the app's "Update Available" banner (applyServiceWorkerUpdate in
// sw-register.ts) activate a waiting worker immediately instead of waiting
// for all tabs to close.
self.addEventListener('message', (event) => {
if (event.data && event.data.type === 'SKIP_WAITING') {
self.skipWaiting();
}
});
setCatchHandler(async ({ event }) => {
if (event.request.mode === 'navigate') {
const cached = await caches.match(OFFLINE_URL);
return cached || Response.error();
}
return Response.error();
});
// 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('/')
);
});