37 lines
1.0 KiB
TypeScript
37 lines
1.0 KiB
TypeScript
"use client";
|
|
|
|
import { useEffect } from "react";
|
|
|
|
export function ServiceWorkerRegistration() {
|
|
useEffect(() => {
|
|
if (typeof window === "undefined" || !("serviceWorker" in navigator)) {
|
|
return;
|
|
}
|
|
|
|
if (process.env.NODE_ENV !== "production") {
|
|
// In development, unregister any previously installed service worker
|
|
// and clear its caches so hot reload always serves fresh assets.
|
|
navigator.serviceWorker.getRegistrations().then((registrations) => {
|
|
registrations.forEach((registration) => registration.unregister());
|
|
});
|
|
if ("caches" in window) {
|
|
caches.keys().then((keys) => {
|
|
keys.forEach((key) => caches.delete(key));
|
|
});
|
|
}
|
|
return;
|
|
}
|
|
|
|
navigator.serviceWorker
|
|
.register("/sw.js")
|
|
.then((registration) => {
|
|
console.log("Service Worker registered successfully:", registration);
|
|
})
|
|
.catch((error) => {
|
|
console.error("Service Worker registration failed:", error);
|
|
});
|
|
}, []);
|
|
|
|
return null;
|
|
}
|