-
-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathservice-worker.js
69 lines (61 loc) · 1.88 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
importScripts('https://storage.googleapis.com/workbox-cdn/releases/5.1.2/workbox-sw.js');
if (workbox) {
console.log(`Yay! Workbox is loaded 🎉`);
initializeWorkbox();
} else {
console.log(`Boo! Workbox didn't load 😬`);
}
// Handle Push Notifications
self.addEventListener('push', event => {
const message = event.data.text()
console.log(`Push received with data "${message}"`);
const title = 'Push Notification';
const options = {
body: `${message}`,
data: { href: '/games' },
actions: [
{ action: 'game1', title: 'Game 1' },
],
};
event.waitUntil(self.registration.showNotification(title, options));
});
// Handle notification clicks
self.addEventListener('notificationclick', event => {
const notification = event.notification;
const action = event.action;
if (action !== 'dismiss') {
// This handles both notification click and 'details' action,
// because some platforms might not support actions.
clients.openWindow(notification.data.href);
}
notification.close();
});
function initializeWorkbox() {
workbox.routing.registerRoute(
/^(?!.*(?:unsplash|giphy|tenor|firebasestorage))(?=.*(?:png|jpg|jpeg|svg|webp|gif)).*/,
new workbox.strategies.CacheFirst({
cacheName: 'images',
plugins: [
new workbox.expiration.ExpirationPlugin({
maxAgeSeconds: 30 * 24 * 60 * 60,
maxEntries: 60,
}),
],
})
);
workbox.routing.registerRoute(
/^(?=.*(?:unsplash|giphy|tenor|firebasestorage))(?=.*(?:png|jpg|jpeg|svg|webp|gif)).*/,
new workbox.strategies.StaleWhileRevalidate({
cacheName: 'cors-images',
plugins: [
new workbox.expiration.ExpirationPlugin({
maxAgeSeconds: 30 * 24 * 60 * 60,
maxEntries: 60,
}),
new workbox.cacheableResponse.CacheableResponsePlugin({
statuses: [0, 200],
}),
],
})
);
}