const isLocalhost = self.location.hostname === "localhost" || self.location.hostname === "127.0.0.1"; if (!isLocalhost) { const STATIC_CACHE = "construction-delivery-static-v5"; const RUNTIME_CACHE = "construction-delivery-runtime-v5"; const APP_SHELL_URLS = ["/", "/index.html", "/manifest.webmanifest", "/icons/icon-192.png", "/icons/icon-512.png"]; self.addEventListener("install", (event) => { event.waitUntil( caches.open(STATIC_CACHE).then((cache) => cache.addAll(APP_SHELL_URLS)).then(() => self.skipWaiting()), ); }); self.addEventListener("activate", (event) => { event.waitUntil( caches .keys() .then((keys) => Promise.all( keys .filter((key) => ![STATIC_CACHE, RUNTIME_CACHE].includes(key)) .map((key) => caches.delete(key)), ), ) .then(() => self.clients.claim()) .then(async () => { const clients = await self.clients.matchAll({ includeUncontrolled: true }); clients.forEach((client) => client.postMessage({ type: "PWA_OFFLINE_READY" })); }), ); }); self.addEventListener("fetch", (event) => { if (event.request.method !== "GET") { return; } const requestUrl = new URL(event.request.url); const isSameOrigin = requestUrl.origin === self.location.origin; if (event.request.mode === "navigate") { event.respondWith( fetch(event.request) .then((response) => { const responseClone = response.clone(); caches.open(RUNTIME_CACHE).then((cache) => cache.put(event.request, responseClone)); return response; }) .catch(async () => { const cachedPage = await caches.match(event.request); return cachedPage || caches.match("/index.html"); }), ); return; } if (!isSameOrigin) { return; } event.respondWith( caches.match(event.request).then((cachedResponse) => { if (cachedResponse) { return cachedResponse; } return fetch(event.request).then((response) => { if (!response || response.status !== 200) { return response; } const responseClone = response.clone(); caches.open(RUNTIME_CACHE).then((cache) => cache.put(event.request, responseClone)); return response; }); }), ); }); } else { self.addEventListener("install", (event) => self.skipWaiting()); self.addEventListener("activate", (event) => self.clients.claim()); } // Push notification handler self.addEventListener("push", (event) => { let data = {}; try { data = event.data ? event.data.json() : {}; } catch { data = { title: "Уведомление", body: "" }; } const title = data.title || "Уведомление"; const options = { body: data.body || "", icon: data.icon || "/icons/icon-192.png", badge: data.badge || "/icons/icon-192.png", data: data.data || {}, tag: data.tag || "default", vibrate: [100, 50, 100], requireInteraction: data.requireInteraction || false, }; event.waitUntil(self.registration.showNotification(title, options)); }); self.addEventListener("notificationclick", (event) => { event.notification.close(); const clickData = event.notification.data || {}; const targetUrl = clickData.order_id ? "/dashboard/group/" + clickData.order_id : "/dashboard"; event.waitUntil( self.clients.matchAll({ type: "window", includeUncontrolled: true }).then((clientList) => { for (const client of clientList) { if (client.url.includes("/dashboard") && "focus" in client) { return client.focus(); } } return self.clients.openWindow(targetUrl); }), ); });