38 lines
1.5 KiB
TypeScript
38 lines
1.5 KiB
TypeScript
'use client';
|
|
|
|
import { useEffect } from 'react';
|
|
|
|
export default function PWARegistration() {
|
|
useEffect(() => {
|
|
if ('serviceWorker' in navigator && window.location.hostname !== 'localhost') {
|
|
window.addEventListener('load', () => {
|
|
navigator.serviceWorker
|
|
.register('/sw.js')
|
|
.then((registration) => {
|
|
console.log('SW registered: ', registration);
|
|
|
|
// Check for updates
|
|
registration.onupdatefound = () => {
|
|
const installingWorker = registration.installing;
|
|
if (installingWorker == null) return;
|
|
installingWorker.onstatechange = () => {
|
|
if (installingWorker.state === 'installed') {
|
|
if (navigator.serviceWorker.controller) {
|
|
console.log('[SW] New content is available; please refresh.');
|
|
} else {
|
|
console.log('[SW] Content is cached for offline use.');
|
|
}
|
|
}
|
|
};
|
|
};
|
|
})
|
|
.catch((registrationError) => {
|
|
console.log('SW registration failed: ', registrationError);
|
|
});
|
|
});
|
|
}
|
|
}, []);
|
|
|
|
return null;
|
|
}
|