testing
This commit is contained in:
@@ -4,55 +4,38 @@ export default defineNuxtPlugin(() => {
|
|||||||
const updateAvailable = ref(false);
|
const updateAvailable = ref(false);
|
||||||
let wb: Workbox | null = null;
|
let wb: Workbox | null = null;
|
||||||
|
|
||||||
// Enhanced PWA detection
|
if ("serviceWorker" in navigator) {
|
||||||
const isStandalone = window.matchMedia(
|
|
||||||
"(display-mode: standalone)"
|
|
||||||
).matches;
|
|
||||||
const isIOSPWA = (window.navigator as any).standalone;
|
|
||||||
const isInstalledAsPWA = isStandalone || isIOSPWA;
|
|
||||||
|
|
||||||
if ("serviceWorker" in navigator && isInstalledAsPWA) {
|
|
||||||
wb = new Workbox("/sw.js");
|
wb = new Workbox("/sw.js");
|
||||||
|
|
||||||
// Listen for version checks from SW
|
// Listen for messages from the service worker
|
||||||
wb.addEventListener("message", (event) => {
|
navigator.serviceWorker.addEventListener("message", (event) => {
|
||||||
if (event.data.type === "VERSION_CHECK") {
|
if (event.data && event.data.type === "VERSION_CHECK") {
|
||||||
checkForUpdate(event.data.version);
|
checkForUpdate(event.data.version);
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
// Register service worker
|
// Register the service worker and check if there's already a waiting one
|
||||||
wb.register().then((registration: any) => {
|
wb.register().then((registration: any) => {
|
||||||
if (registration.waiting) {
|
if (registration.waiting) {
|
||||||
checkForUpdate();
|
checkForUpdate();
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
// Detect controller changes for updates
|
|
||||||
wb.addEventListener("controlling", () => {
|
|
||||||
window.location.reload();
|
|
||||||
});
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// 🔹 Function to compare versions and show update modal if needed
|
||||||
const checkForUpdate = (newVersion?: string) => {
|
const checkForUpdate = (newVersion?: string) => {
|
||||||
const currentVersion = localStorage.getItem("pwa_version");
|
const currentVersion = localStorage.getItem("pwa_version");
|
||||||
|
|
||||||
if (!newVersion) {
|
if (newVersion && currentVersion !== newVersion) {
|
||||||
// If no version provided, just trigger update
|
|
||||||
updateAvailable.value = true;
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (currentVersion !== newVersion) {
|
|
||||||
updateAvailable.value = true;
|
updateAvailable.value = true;
|
||||||
localStorage.setItem("pwa_version", newVersion);
|
localStorage.setItem("pwa_version", newVersion);
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
// 🔹 Function to apply the update
|
||||||
const handleUpdate = () => {
|
const handleUpdate = () => {
|
||||||
if (!!wb) {
|
wb?.messageSW({ type: "SKIP_WAITING" });
|
||||||
wb.messageSW({ type: "SKIP_WAITING" });
|
window.location.reload();
|
||||||
}
|
|
||||||
};
|
};
|
||||||
|
|
||||||
return {
|
return {
|
||||||
|
|||||||
+8
-19
@@ -5,23 +5,11 @@ precacheAndRoute(self.__WB_MANIFEST);
|
|||||||
|
|
||||||
// Version
|
// Version
|
||||||
|
|
||||||
const VERSION = "1.0.6";
|
const VERSION = "1.0.7";
|
||||||
|
|
||||||
// Service Worker Installation
|
// Service Worker Installation
|
||||||
self.addEventListener("install", (event) => {
|
self.addEventListener("install", (event) => {
|
||||||
self.skipWaiting();
|
self.skipWaiting();
|
||||||
// Clear old cache during install
|
|
||||||
event.waitUntil(
|
|
||||||
caches.keys().then((cacheNames) => {
|
|
||||||
return Promise.all(
|
|
||||||
cacheNames.map((cache) => {
|
|
||||||
if (cache !== self.registration.scope + "::" + VERSION) {
|
|
||||||
return caches.delete(cache);
|
|
||||||
}
|
|
||||||
})
|
|
||||||
);
|
|
||||||
})
|
|
||||||
);
|
|
||||||
});
|
});
|
||||||
|
|
||||||
// Service Worker Activation
|
// Service Worker Activation
|
||||||
@@ -29,13 +17,14 @@ self.addEventListener("activate", (event) => {
|
|||||||
event.waitUntil(
|
event.waitUntil(
|
||||||
(async () => {
|
(async () => {
|
||||||
const clients = await self.clients.matchAll({ type: "window" });
|
const clients = await self.clients.matchAll({ type: "window" });
|
||||||
clients.forEach((client) => {
|
|
||||||
client.postMessage({
|
// Notify all open clients about the version
|
||||||
type: "VERSION_CHECK",
|
clients.forEach((client) =>
|
||||||
version: VERSION,
|
client.postMessage({ type: "VERSION_CHECK", version: VERSION })
|
||||||
});
|
);
|
||||||
});
|
|
||||||
self.clients.claim();
|
self.clients.claim();
|
||||||
|
console.log("Service Worker Activated (Version: " + VERSION + ")");
|
||||||
})()
|
})()
|
||||||
);
|
);
|
||||||
});
|
});
|
||||||
|
|||||||
Reference in New Issue
Block a user