From c3d507261467ed5be1827f611f5248484215a852 Mon Sep 17 00:00:00 2001 From: FireMasterK <20838718+FireMasterK@users.noreply.github.com> Date: Mon, 16 Nov 2020 16:06:37 +0530 Subject: [PATCH] Automatically update service worker. --- src/registerServiceWorker.js | 56 +++++++++++++++++++----------------- 1 file changed, 30 insertions(+), 26 deletions(-) diff --git a/src/registerServiceWorker.js b/src/registerServiceWorker.js index 76cede07..9fc727ff 100644 --- a/src/registerServiceWorker.js +++ b/src/registerServiceWorker.js @@ -3,30 +3,34 @@ import { register } from 'register-service-worker' if (process.env.NODE_ENV === 'production') { - register(`${process.env.BASE_URL}service-worker.js`, { - ready () { - console.log( - 'App is being served from cache by a service worker.\n' + - 'For more details, visit https://goo.gl/AFskqB' - ) - }, - registered () { - console.log('Service worker has been registered.') - }, - cached () { - console.log('Content has been cached for offline use.') - }, - updatefound () { - console.log('New content is downloading.') - }, - updated () { - console.log('New content is available; please refresh.') - }, - offline () { - console.log('No internet connection found. App is running in offline mode.') - }, - error (error) { - console.error('Error during service worker registration:', error) - } - }) + register(`${process.env.BASE_URL}service-worker.js`, { + ready() { + console.log( + 'App is being served from cache by a service worker.\n' + + 'For more details, visit https://goo.gl/AFskqB' + ) + }, + registered() { + console.log('Service worker has been registered.') + }, + cached() { + console.log('Content has been cached for offline use.') + }, + updatefound() { + console.log('New content is downloading.') + }, + updated() { + console.log('New content is available; please refresh.') + caches.keys().then(function(names) { + for (let name of names) caches.delete(name); + }); + window.location.reload() + }, + offline() { + console.log('No internet connection found. App is running in offline mode.') + }, + error(error) { + console.error('Error during service worker registration:', error) + } + }) }