forked from le0pard/pgtune
-
Notifications
You must be signed in to change notification settings - Fork 0
/
swWindow.js
31 lines (24 loc) · 910 Bytes
/
swWindow.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
import { Workbox, messageSW } from 'workbox-window'
import { readyToUpdated } from 'reducers/sw'
let wb = null
let wbRegistration = null
export const skipWaitingMessageAndReload = () => {
wb.addEventListener('controlling', () => window.location.reload())
if (wbRegistration && wbRegistration.waiting) {
// Send a message to the waiting service worker,
// instructing it to activate.
messageSW(wbRegistration.waiting, { type: 'SKIP_WAITING' })
}
}
export const initServiceWorker = (store) => {
if ('serviceWorker' in navigator) {
wb = new Workbox('/sw.js')
const showUpdatedInfo = () => store.dispatch(readyToUpdated())
wb.addEventListener('waiting', showUpdatedInfo)
wb.addEventListener('externalwaiting', showUpdatedInfo)
// Register the service worker after event listeners have been added.
wb.register().then((r) => {
wbRegistration = r
})
}
}