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 FROM node:lts-alpine as builder
COPY ./frontend . COPY ./frontend .
ENV NODE_ENV=production
RUN npm install RUN npm install
RUN npm run build RUN npm run build
FROM python:3.7-slim-stretch FROM python:3.7-slim-stretch

View File

@ -2,12 +2,6 @@
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 () {
@ -27,7 +21,8 @@ if (process.env.NODE_ENV === 'production') {
}, },
updated (registration) { updated (registration) {
console.log('New content is available; please refresh.') console.log('New content is available; please refresh.')
notifyAboutUpdate(registration.waiting) let confirmationResult = confirm("New content found! Do you want to reload the app?")
if (confirmationResult) registration.waiting.postMessage({action: "skipWaiting"})
}, },
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.')
@ -37,10 +32,10 @@ if (process.env.NODE_ENV === 'production') {
} }
}) })
var refreshing; let refreshing
navigator.serviceWorker.addEventListener("controllerchange", function () { navigator.serviceWorker.addEventListener("controllerchange", e=>{
if (refreshing) return; if (refreshing) return
window.location.reload(); window.location.reload()
refreshing = true; refreshing = true
}) })
} }

View File

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