-
Notifications
You must be signed in to change notification settings - Fork 2
/
custom-service-worker.js
40 lines (36 loc) · 1.17 KB
/
custom-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
import { precacheAndRoute } from 'workbox-precaching'
import { registerRoute } from 'workbox-routing'
import { NetworkFirst } from 'workbox-strategies'
// Disable workbox logs
self.__WB_DISABLE_DEV_LOGS = true
// Activate new service worker
self.addEventListener('message', event => {
if (event.data && event.data.type === 'SKIP_WAITING') {
self.skipWaiting()
}
})
// Caching for offline mode
// Preload and cache all resources defined in the manifest
precacheAndRoute(self.__WB_MANIFEST)
// Register the `NetworkFirst` caching strategy for all HTTP requests
registerRoute(
({url}) => url.href.startsWith('http'),
new NetworkFirst()
)
// Web push notification
let clickOpenUrl
self.addEventListener('push', event => {
const pushOptions = event.data.json()
clickOpenUrl = pushOptions.url
// Show notification
event.waitUntil(self.registration.showNotification(pushOptions.title, pushOptions))
})
self.addEventListener('notificationclick', event => {
// Close notification if clicked
event.notification.close()
// Open window on the specified url
if (clickOpenUrl) {
const promiseChain = clients.openWindow(clickOpenUrl)
event.waitUntil(promiseChain)
}
})