👔 SW now disabled by default

This commit is contained in:
Alicia Sykes 2021-09-12 15:00:10 +01:00
parent 21014cf0cd
commit f772eb5854
1 changed files with 26 additions and 22 deletions

View File

@ -1,7 +1,6 @@
/* eslint-disable no-console */
import { register } from 'register-service-worker'; import { register } from 'register-service-worker';
import { sessionStorageKeys } from '@/utils/defaults'; import { sessionStorageKeys } from '@/utils/defaults';
import { statusMsg, statusErrorMsg } from '@/utils/CoolConsole';
import conf from '../../public/conf.yml'; import conf from '../../public/conf.yml';
/* Sets a local storage item with the state from the SW lifecycle */ /* Sets a local storage item with the state from the SW lifecycle */
@ -23,7 +22,7 @@ const setSwStatus = (swStateToSet) => {
const newSwState = { ...currentSwState, ...swStateToSet }; const newSwState = { ...currentSwState, ...swStateToSet };
sessionStorage.setItem(sessionStorageKeys.SW_STATUS, JSON.stringify(newSwState)); sessionStorage.setItem(sessionStorageKeys.SW_STATUS, JSON.stringify(newSwState));
} catch (e) { } catch (e) {
console.warn('Error setting SW data', e); statusErrorMsg('Service Worker Status', 'Error Updating SW Status', e);
} }
}; };
@ -33,53 +32,58 @@ const setSwStatus = (swStateToSet) => {
* Or disable if user specified to disable * Or disable if user specified to disable
*/ */
const shouldEnableServiceWorker = () => { const shouldEnableServiceWorker = () => {
let shouldEnable = true; if (conf && conf.appConfig && conf.appConfig.enableServiceWorker) {
if (conf && conf.appConfig) { // Check if app Config available setSwStatus({ disabledByUser: false });
if (conf.appConfig.disableServiceWorker) { // Disable if user requested return true;
shouldEnable = false; } else if (process.env.NODE_ENV !== 'production') {
setSwStatus({ disabledByUser: true });
}
}
if (process.env.NODE_ENV !== 'production') {
shouldEnable = false; // Disable if not in production
setSwStatus({ devMode: true }); setSwStatus({ devMode: true });
return false;
} }
return shouldEnable; setSwStatus({ disabledByUser: true });
return false;
}; };
/* Calls to the print status function */
const printSwStatus = (msg) => {
statusMsg('Service Worker Status', msg);
};
const swUrl = `${process.env.BASE_URL || '/'}service-worker.js`;
/* If service worker enabled, then register it, and print message when status changes */
const registerServiceWorker = () => { const registerServiceWorker = () => {
if (shouldEnableServiceWorker()) { if (shouldEnableServiceWorker()) {
register(`${process.env.BASE_URL}service-worker.js`, { register(swUrl, {
ready() { ready() {
setSwStatus({ ready: true }); setSwStatus({ ready: true });
console.log( printSwStatus(
'App is being served from cache by a service worker.\n' 'Dashy is being served from cache by a service worker.\n'
+ 'For more details, visit https://goo.gl/AFskqB', + 'For more details, visit https://goo.gl/AFskqB',
); );
}, },
registered() { registered() {
setSwStatus({ registered: true }); setSwStatus({ registered: true });
console.log('Service worker has been registered.'); printSwStatus('Service worker has been registered.');
}, },
cached() { cached() {
setSwStatus({ cached: true }); setSwStatus({ cached: true });
console.log('Content has been cached for offline use.'); printSwStatus('App has been cached for offline use.');
}, },
updatefound() { updatefound() {
setSwStatus({ updateFound: true }); setSwStatus({ updateFound: true });
console.log('New content is downloading.'); printSwStatus('New content is downloading...');
}, },
updated() { updated() {
setSwStatus({ updated: true }); setSwStatus({ updated: true });
console.log('New content is available; please refresh.'); printSwStatus('New content is available; please refresh the page.');
}, },
offline() { offline() {
setSwStatus({ offline: true }); setSwStatus({ offline: true });
console.log('No internet connection found. App is running in offline mode.'); printSwStatus('No internet connection found. Dashy is running in offline mode.');
}, },
error(error) { error(error) {
setSwStatus({ error: true }); setSwStatus({ error: true });
console.error('Error during service worker registration:', error); statusErrorMsg('Service Worker Status', 'Error during SW registration', error);
}, },
}); });
} }