Enhance service worker with update handling and improve task scheduling format
This commit is contained in:
@@ -3,6 +3,24 @@ if ('serviceWorker' in navigator) {
|
||||
navigator.serviceWorker.register('/service-worker.js')
|
||||
.then(registration => {
|
||||
console.log('ServiceWorker registration successful with scope: ', registration.scope);
|
||||
|
||||
registration.onupdatefound = () => {
|
||||
const installingWorker = registration.installing;
|
||||
installingWorker.onstatechange = () => {
|
||||
if (installingWorker.state === 'installed') {
|
||||
if (navigator.serviceWorker.controller) {
|
||||
// New update available
|
||||
console.log('New content is available; please refresh.');
|
||||
if (confirm('New version available. Do you want to update?')) {
|
||||
window.location.reload();
|
||||
}
|
||||
} else {
|
||||
// Content is cached for offline use
|
||||
console.log('Content is cached for offline use.');
|
||||
}
|
||||
}
|
||||
};
|
||||
};
|
||||
})
|
||||
.catch(error => {
|
||||
console.log('ServiceWorker registration failed: ', error);
|
||||
|
||||
Reference in New Issue
Block a user