From d565a1143ca7407b7802d376e6515cdf609d6251 Mon Sep 17 00:00:00 2001 From: Alicia Sykes Date: Tue, 22 Jun 2021 22:29:48 +0100 Subject: [PATCH] :construction: WIP, Passes service worker state to app --- src/registerServiceWorker.js | 92 +++++++++++++++++++++++++----------- 1 file changed, 64 insertions(+), 28 deletions(-) diff --git a/src/registerServiceWorker.js b/src/registerServiceWorker.js index e32aa92b..bf9af2f5 100644 --- a/src/registerServiceWorker.js +++ b/src/registerServiceWorker.js @@ -1,32 +1,68 @@ /* eslint-disable no-console */ import { register } from 'register-service-worker'; +import { sessionStorageKeys } from './utils/defaults'; -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); - }, - }); -} +/* Sets a local storage item with the state from the SW lifecycle */ +const setSwStatus = (swStateToSet) => { + const initialSwState = { + ready: false, + registered: false, + cached: false, + updateFound: false, + updated: false, + offline: false, + error: false, + devMode: false, + }; + const sessionData = sessionStorage[sessionStorageKeys.SW_STATUS]; + const currentSwState = sessionData ? JSON.parse(sessionData) : initialSwState; + try { + const newSwState = { ...currentSwState, ...swStateToSet }; + sessionStorage.setItem(sessionStorageKeys.SW_STATUS, JSON.stringify(newSwState)); + } catch (e) { + console.warn('Error setting SW data', e); + } +}; + +const registerServiceWorker = () => { + if (process.env.NODE_ENV === 'production') { + register(`${process.env.BASE_URL}service-worker.js`, { + ready() { + setSwStatus({ ready: true }); + console.log( + 'App is being served from cache by a service worker.\n' + + 'For more details, visit https://goo.gl/AFskqB', + ); + }, + registered() { + setSwStatus({ registered: true }); + console.log('Service worker has been registered.'); + }, + cached() { + setSwStatus({ cached: true }); + console.log('Content has been cached for offline use.'); + }, + updatefound() { + setSwStatus({ updateFound: true }); + console.log('New content is downloading.'); + }, + updated() { + setSwStatus({ updated: true }); + console.log('New content is available; please refresh.'); + }, + offline() { + setSwStatus({ offline: true }); + console.log('No internet connection found. App is running in offline mode.'); + }, + error(error) { + setSwStatus({ error: true }); + console.error('Error during service worker registration:', error); + }, + }); + } else { // Not in production, don't use SW + setSwStatus({ devMode: true }); + } +}; + +export default registerServiceWorker;