|
import * as SnackBar from 'node-snackbar'; |
|
|
|
// .... |
|
|
|
// Service Worker |
|
// https://github.com/GoogleChrome/sw-precache/blob/master/demo/app/js/service-worker-registration.js |
|
|
|
const offlineMsg = 'Vous êtes passé(e) en mode déconnecté.'; |
|
const onlineMsg = 'Vous êtes de nouveau connecté(e).'; |
|
const redundantMsg = 'SW : The installing service worker became redundant.'; |
|
const errorMsg = 'SW : Error during service worker registration : '; |
|
const refreshMsg = 'Du nouveau contenu est disponible sur le site, vous pouvez y accéder en rafraichissant cette page.'; |
|
const availableMsg = 'SW : Content is now available offline.'; |
|
const close = 'Fermer'; |
|
const refresh = 'Rafraîchir'; |
|
|
|
if ('serviceWorker' in navigator) { |
|
window.addEventListener('load', () => { |
|
function updateOnlineStatus() { |
|
SnackBar.show({ |
|
text: navigator.onLine ? onlineMsg : offlineMsg, |
|
backgroundColor: '#000000', |
|
actionText: close, |
|
}); |
|
} |
|
window.addEventListener('online', updateOnlineStatus); |
|
window.addEventListener('offline', updateOnlineStatus); |
|
navigator.serviceWorker.register('sw.js').then((reg) => { |
|
reg.onupdatefound = () => { |
|
const installingWorker = reg.installing; |
|
installingWorker.onstatechange = () => { |
|
switch (installingWorker.state) { |
|
case 'installed': |
|
if (navigator.serviceWorker.controller) { |
|
SnackBar.show({ |
|
text: refreshMsg, |
|
backgroundColor: '#000000', |
|
actionText: refresh, |
|
onActionClick: () => { location.reload(); }, |
|
}); |
|
} else { |
|
console.info(availableMsg); |
|
} |
|
break; |
|
case 'redundant': |
|
console.info(redundantMsg); |
|
break; |
|
default: |
|
break; |
|
} |
|
}; |
|
}; |
|
}).catch((e) => { |
|
console.error(errorMsg, e); |
|
}); |
|
}); |
|
} |
|
|
|
// .... |