tried fixing infinite loop
This commit is contained in:
@@ -8,29 +8,73 @@ export default defineNuxtPlugin(() => {
|
||||
const { isInstalledAsPWA } = usePWA();
|
||||
|
||||
if ("serviceWorker" in navigator && isInstalledAsPWA.value) {
|
||||
// Initialize Workbox
|
||||
wb = new Workbox("/sw.js");
|
||||
|
||||
wb.addEventListener("waiting", () => {
|
||||
checkForUpdate();
|
||||
});
|
||||
navigator.serviceWorker
|
||||
.register("/sw.js")
|
||||
.then((registration) => {
|
||||
// Native Service Worker API for update detection
|
||||
registration.addEventListener("updatefound", () => {
|
||||
const newWorker = registration.installing;
|
||||
if (newWorker) {
|
||||
newWorker.addEventListener("statechange", () => {
|
||||
if (newWorker.state === "installed") {
|
||||
// Only show prompt if there's a controller (not first install)
|
||||
if (navigator.serviceWorker.controller) {
|
||||
checkForUpdate();
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
});
|
||||
|
||||
wb.register()
|
||||
.then((registration: any) => {
|
||||
// Workbox events for consistency
|
||||
wb?.addEventListener("waiting", () => {
|
||||
checkForUpdate();
|
||||
});
|
||||
|
||||
// Check if there's already a waiting worker
|
||||
if (registration.waiting) {
|
||||
checkForUpdate();
|
||||
}
|
||||
|
||||
// Periodic update checks (optional)
|
||||
setInterval(() => {
|
||||
registration.update().catch((err) => {
|
||||
console.debug(
|
||||
"Service worker update check failed:",
|
||||
err
|
||||
);
|
||||
});
|
||||
}, 60 * 60 * 1000); // Check every hour
|
||||
})
|
||||
.catch((error) => {
|
||||
console.error("Service worker registration failed:", error);
|
||||
.catch((err) => {
|
||||
console.error("Service worker registration failed:", err);
|
||||
});
|
||||
|
||||
// Register Workbox
|
||||
wb.register().catch((error) => {
|
||||
console.error("Workbox registration failed:", error);
|
||||
});
|
||||
}
|
||||
|
||||
const checkForUpdate = () => {
|
||||
updateAvailable.value = true;
|
||||
if (!updateAvailable.value) {
|
||||
updateAvailable.value = true;
|
||||
}
|
||||
};
|
||||
|
||||
const handleUpdate = () => {
|
||||
if (wb) {
|
||||
// Send skip waiting message
|
||||
wb.messageSW({ type: "SKIP_WAITING" }).then(() => {
|
||||
// Notify all tabs to reload
|
||||
if (navigator.serviceWorker.controller) {
|
||||
navigator.serviceWorker.controller.postMessage({
|
||||
type: "CLIENT_RELOAD",
|
||||
});
|
||||
}
|
||||
window.location.reload();
|
||||
});
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user