try adding update button again

This commit is contained in:
Alberto Xamin 2021-06-07 19:46:14 +02:00
parent b7096d8646
commit 98d64800fc
No known key found for this signature in database
GPG Key ID: 4F026F48309500A2
3 changed files with 27 additions and 12 deletions

View File

@ -2,6 +2,12 @@
import { register } from 'register-service-worker' import { register } from 'register-service-worker'
const notifyAboutUpdate = worker => {
if (confirm('New version available, update?')) {
worker.postMessage({ action : "skipWaiting"})
}
}
if (process.env.NODE_ENV === 'production') { if (process.env.NODE_ENV === 'production') {
register(`${process.env.BASE_URL}service-worker.js`, { register(`${process.env.BASE_URL}service-worker.js`, {
ready () { ready () {
@ -19,8 +25,9 @@ if (process.env.NODE_ENV === 'production') {
updatefound () { updatefound () {
console.log('New content is downloading.') console.log('New content is downloading.')
}, },
updated () { updated (registration) {
console.log('New content is available; please refresh.') console.log('New content is available; please refresh.')
notifyAboutUpdate(registration.waiting)
}, },
offline () { offline () {
console.log('No internet connection found. App is running in offline mode.') console.log('No internet connection found. App is running in offline mode.')
@ -29,4 +36,11 @@ if (process.env.NODE_ENV === 'production') {
console.error('Error during service worker registration:', error) console.error('Error during service worker registration:', error)
} }
}) })
var refreshing;
navigator.serviceWorker.addEventListener("controllerchange", function () {
if (refreshing) return;
window.location.reload();
refreshing = true;
})
} }

View File

@ -0,0 +1,9 @@
self.__precacheManifest = [].concat(self.__precacheManifest || []);
workbox.precaching.suppressWarnings()
workbox.precaching.precacheAndRoute(self.__precacheManifest, {})
self.addEventListener('message', msg => {
if (msg.data.action == 'skipwaiting') {
self.skipWaiting();
}
})

View File

@ -1,5 +1,3 @@
const { GenerateSW } = require("workbox-webpack-plugin");
module.exports = { module.exports = {
publicPath: "./", publicPath: "./",
pwa: { pwa: {
@ -10,16 +8,10 @@ module.exports = {
}, },
workboxPluginMode: 'InjectManifest', workboxPluginMode: 'InjectManifest',
workboxOptions: { workboxOptions: {
swSrc: 'src/registerServiceWorker.js' swSrc: 'src/service-worker.js'
} }
}, },
configureWebpack: { configureWebpack: {
plugins: [new GenerateSW({
clientsClaim: true,
skipWaiting: true,
cleanupOutdatedCaches: true,
navigateFallback: 'index.html',
})],
output: { output: {
crossOriginLoading: 'anonymous' crossOriginLoading: 'anonymous'
}, },