mirror of
https://github.com/Lissy93/dashy.git
synced 2024-11-23 21:23:28 +03:00
🚧 WIP, Passes service worker state to app
This commit is contained in:
parent
fd18ac0b46
commit
d565a1143c
@ -1,32 +1,68 @@
|
|||||||
/* eslint-disable no-console */
|
/* eslint-disable no-console */
|
||||||
|
|
||||||
import { register } from 'register-service-worker';
|
import { register } from 'register-service-worker';
|
||||||
|
import { sessionStorageKeys } from './utils/defaults';
|
||||||
|
|
||||||
|
/* 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') {
|
if (process.env.NODE_ENV === 'production') {
|
||||||
register(`${process.env.BASE_URL}service-worker.js`, {
|
register(`${process.env.BASE_URL}service-worker.js`, {
|
||||||
ready() {
|
ready() {
|
||||||
|
setSwStatus({ ready: true });
|
||||||
console.log(
|
console.log(
|
||||||
'App is being served from cache by a service worker.\n'
|
'App 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 });
|
||||||
console.log('Service worker has been registered.');
|
console.log('Service worker has been registered.');
|
||||||
},
|
},
|
||||||
cached() {
|
cached() {
|
||||||
|
setSwStatus({ cached: true });
|
||||||
console.log('Content has been cached for offline use.');
|
console.log('Content has been cached for offline use.');
|
||||||
},
|
},
|
||||||
updatefound() {
|
updatefound() {
|
||||||
|
setSwStatus({ updateFound: true });
|
||||||
console.log('New content is downloading.');
|
console.log('New content is downloading.');
|
||||||
},
|
},
|
||||||
updated() {
|
updated() {
|
||||||
|
setSwStatus({ updated: true });
|
||||||
console.log('New content is available; please refresh.');
|
console.log('New content is available; please refresh.');
|
||||||
},
|
},
|
||||||
offline() {
|
offline() {
|
||||||
|
setSwStatus({ offline: true });
|
||||||
console.log('No internet connection found. App is running in offline mode.');
|
console.log('No internet connection found. App is running in offline mode.');
|
||||||
},
|
},
|
||||||
error(error) {
|
error(error) {
|
||||||
|
setSwStatus({ error: true });
|
||||||
console.error('Error during service worker registration:', error);
|
console.error('Error during service worker registration:', error);
|
||||||
},
|
},
|
||||||
});
|
});
|
||||||
|
} else { // Not in production, don't use SW
|
||||||
|
setSwStatus({ devMode: true });
|
||||||
}
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
export default registerServiceWorker;
|
||||||
|
Loading…
Reference in New Issue
Block a user