Initial commit
This commit is contained in:
35
static/register-sw.js
Normal file
35
static/register-sw.js
Normal file
@@ -0,0 +1,35 @@
|
||||
// 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);
|
||||
});
|
||||
});
|
||||
}
|
||||
Reference in New Issue
Block a user