trying to fix service worker

This commit is contained in:
Alberto Xamin 2021-06-07 20:08:02 +02:00
parent 98d64800fc
commit 6a23a1212a
No known key found for this signature in database
GPG Key ID: 4F026F48309500A2
3 changed files with 19 additions and 23 deletions

View File

@ -1,5 +1,6 @@
FROM node:lts-alpine as builder
COPY ./frontend .
ENV NODE_ENV=production
RUN npm install
RUN npm run build
FROM python:3.7-slim-stretch

View File

@ -2,12 +2,6 @@
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') {
register(`${process.env.BASE_URL}service-worker.js`, {
ready () {
@ -26,8 +20,9 @@ if (process.env.NODE_ENV === 'production') {
console.log('New content is downloading.')
},
updated (registration) {
console.log('New content is available; please refresh.')
notifyAboutUpdate(registration.waiting)
console.log('New content is available; please refresh.')
let confirmationResult = confirm("New content found! Do you want to reload the app?")
if (confirmationResult) registration.waiting.postMessage({action: "skipWaiting"})
},
offline () {
console.log('No internet connection found. App is running in offline mode.')
@ -35,12 +30,12 @@ if (process.env.NODE_ENV === 'production') {
error (error) {
console.error('Error during service worker registration:', error)
}
})
})
var refreshing;
navigator.serviceWorker.addEventListener("controllerchange", function () {
if (refreshing) return;
window.location.reload();
refreshing = true;
})
let refreshing
navigator.serviceWorker.addEventListener("controllerchange", e=>{
if (refreshing) return
window.location.reload()
refreshing = true
})
}

View File

@ -1,9 +1,9 @@
self.__precacheManifest = [].concat(self.__precacheManifest || []);
workbox.precaching.suppressWarnings()
workbox.precaching.precacheAndRoute(self.__precacheManifest, {})
workbox.precaching.precacheAndRoute(self.__precacheManifest, {});
self.addEventListener('message', msg => {
if (msg.data.action == 'skipwaiting') {
self.skipWaiting();
}
// install new service worker when ok, then reload page.
self.addEventListener("message", msg=>{
if (msg.data.action=='skipWaiting'){
self.skipWaiting()
}
})