File Manager
Editing: service-worker.js
const CACHE_NAME = "eizon-cache-v1"; const DB_NAME = "eizon-notifications"; const STORE_NAME = "notifications"; const OFFLINE_PAGE = "/offline"; // Path to the offline page // Files to cache const urlsToCache = [ "/allproducts", "/addproduct", "/requests_page", "/e-commerce.css", "/e-commerce.js", "/site.webmanifest", "/android-chrome-192x192.png", "/android-chrome-512x512.png", OFFLINE_PAGE, ]; // Install & Cache Files self.addEventListener("install", (event) => { event.waitUntil( caches .open(CACHE_NAME) .then((cache) => cache.addAll(urlsToCache)) .catch((err) => console.error("Failed to cache on install", err)) ); self.skipWaiting(); // Activate worker immediately }); // Activate & Remove Old Cache self.addEventListener("activate", (event) => { event.waitUntil( caches.keys().then((cacheNames) => { return Promise.all( cacheNames .filter((name) => name !== CACHE_NAME) .map((name) => caches.delete(name)) ); }) ); self.clients.claim(); // Listen for when the client comes online and sync notifications self.clients.matchAll().then((clients) => { clients.forEach((client) => { client.addEventListener("online", () => { console.log("User came online, syncing missed notifications..."); self.registration.sync.register("sync-notifications"); }); }); }); }); // Fetch & Update Cache self.addEventListener("fetch", (event) => { event.respondWith( fetch(event.request) .then((response) => { const responseClone = response.clone(); caches.open(CACHE_NAME).then((cache) => { cache.put(event.request, responseClone); }); return response; }) .catch(() => caches.match(event.request).then((cacheResponse) => cacheResponse || caches.match(OFFLINE_PAGE))) ); }); // IndexedDB Functions function openDB() { return new Promise((resolve, reject) => { const request = indexedDB.open(DB_NAME, 1); request.onupgradeneeded = (event) => { const db = event.target.result; if (!db.objectStoreNames.contains(STORE_NAME)) { db.createObjectStore(STORE_NAME, { keyPath: "id", autoIncrement: true }); } }; request.onsuccess = () => resolve(request.result); request.onerror = () => reject(request.error); }); } function storeNotification(notification) { return openDB().then((db) => { const transaction = db.transaction(STORE_NAME, "readwrite"); return transaction.objectStore(STORE_NAME).add(notification); }); } function getNotifications() { return openDB().then((db) => { const transaction = db.transaction(STORE_NAME, "readonly"); return transaction.objectStore(STORE_NAME).getAll(); }); } function clearNotifications() { return openDB().then((db) => { const transaction = db.transaction(STORE_NAME, "readwrite"); return transaction.objectStore(STORE_NAME).clear(); }); } // Listen for push events self.addEventListener("push", (event) => { console.log("Push event received:", event); let data = {}; try { data = event.data?.json() || {}; } catch (err) { console.error("Failed to parse push event data:", err); data = { title: "EIZON", body: "Check out the latest on EIZON!", url: "/" }; } const title = data.title || "EIZON"; const options = { body: data.body || "You have a new update!", icon: "/android-chrome-192x192.png", badge: "/android-chrome-192x192.png", data: { url: data.url || "/" }, }; if (!navigator.onLine) { event.waitUntil( storeNotification({ title, options }) .then(() => console.log("Notification stored in IndexedDB")) .catch((err) => console.error("Failed to store notification:", err)) ); } else { event.waitUntil(self.registration.showNotification(title, options)); } }); // Handle notification click self.addEventListener("notificationclick", (event) => { console.log("Notification clicked:", event.notification); event.notification.close(); const urlToOpen = event.notification.data.url || "/"; event.waitUntil( clients.matchAll({ type: "window", includeUncontrolled: true }).then((clientList) => { for (const client of clientList) { if (client.url.includes(urlToOpen) && "focus" in client) { return client.focus(); } } return clients.openWindow(urlToOpen); }) ); }); // Listen for sync events self.addEventListener("sync", (event) => { if (event.tag === "sync-notifications") { console.log("Syncing missed notifications..."); event.waitUntil( getNotifications() .then((notifications) => { if (notifications.length === 0) { console.log("No missed notifications to sync."); return; } notifications.forEach((notification) => { self.registration.showNotification(notification.title, notification.options); }); return clearNotifications(); }) .then(() => console.log("Missed notifications displayed and cleared")) .catch((err) => console.error("Failed to sync notifications:", err)) ); } });
💾 Save
⬅ Back