35 lines
1.3 KiB
JavaScript
35 lines
1.3 KiB
JavaScript
// Check if service workers are supported
|
|
if ('serviceWorker' in navigator) {
|
|
window.addEventListener('load', () => {
|
|
navigator.serviceWorker.register('/service-worker.js')
|
|
.then(registration => {
|
|
console.log('Service Worker registered with scope:', registration.scope);
|
|
|
|
// Check for updates to the Service Worker
|
|
registration.addEventListener('updatefound', () => {
|
|
const newWorker = registration.installing;
|
|
console.log('Service Worker update found!');
|
|
|
|
newWorker.addEventListener('statechange', () => {
|
|
if (newWorker.state === 'installed') {
|
|
if (navigator.serviceWorker.controller) {
|
|
// New content is available, notify user
|
|
console.log('New version available! Reload to update.');
|
|
|
|
// Optionally, display a notification to the user
|
|
if (window.confirm('A new version of this app is available. Reload to update?')) {
|
|
window.location.reload();
|
|
}
|
|
} else {
|
|
// First time install
|
|
console.log('App is now available offline!');
|
|
}
|
|
}
|
|
});
|
|
});
|
|
})
|
|
.catch(error => {
|
|
console.error('Service Worker registration failed:', error);
|
|
});
|
|
});
|
|
} |