Files
Portfolio/src/utils/serviceWorkerRegistration.ts
T

41 lines
1.2 KiB
TypeScript

/**
* Service Worker Registration Utility
* Handles registration and unregistration of service workers for PWA functionality
*/
/**
* Registers the service worker
* Automatically registers the service worker after the window load event
*/
export function register() {
if ('serviceWorker' in navigator) {
window.addEventListener('load', () => {
const swUrl = `${import.meta.env.BASE_URL}sw.js`;
navigator.serviceWorker
.register(swUrl)
.then((_registration) => {
console.log('ServiceWorker registration successful');
})
.catch((error) => {
console.error('ServiceWorker registration failed:', error);
});
});
}
}
/**
* Unregisters the service worker
* Removes the currently registered service worker
*/
export function unregister() {
if ('serviceWorker' in navigator) {
navigator.serviceWorker.ready
.then((registration) => {
registration.unregister();
})
.catch((error) => {
console.error(error.message);
});
}
}