Created by: piotr-cz
This is PR version of #3375 for next
branch.
Adding two callbacks in serviceWorker.js
file:
-
onUpdate
- executed when service worker has been updated in the background (console logs New content is available; please refresh.) -
onSuccess
- executed after service worker has been installed for the first time (console logs Content is cached for offline use.)
To use it inside application to show notification (toast/ icon etc) register service worker inside App.js file:
// App.js
import { register as registerServiceWorker } from './serviceWorker.js'
export default class App {
constructor() {
// Register service worker and add onUpdate callback
registerServiceWorker({
onUpdate: this.handleServiceWorkerUpdate
})
}
// Simplest notification implementation example.
handleServiceWorkerUpdate(registration) {
if (window.confirm('New update has been installed, click to restart')) {
window.location.reload()
}
}
}