/* AlterFlixHub Service Worker — PWA * ------------------------------------------------------------ * ★ หน้าเว็บ (navigation) : network-first → ออฟไลน์ใช้แคช * ★ ข้อมูล JSON (/data/) : network-first → กันข้อมูลค้างหลังแก้ในหลังบ้าน * ★ ไฟล์นิ่ง (_assets, svg): cache-first + อัปเดตพื้นหลัง * ★ ข้าม /_agent-native/ (API ของแพลตฟอร์ม) — ห้ามแคช * ★ อัปเดตเวอร์ชัน: bump CACHE_VERSION แล้ว reload ครั้งเดียวตอน controllerchange */ const CACHE_VERSION = "alterflixhub-v1.1.0"; const PRECACHE = [ "/", "/admin", "/data/theaters.json", "/manifest.json", "/favicon.svg", "/icon-192.svg", "/icon-512.svg", ]; self.addEventListener("install", (event) => { event.waitUntil( (async () => { const cache = await caches.open(CACHE_VERSION); await Promise.all( PRECACHE.map((url) => cache.add(new Request(url, { cache: "reload" })).catch(() => undefined), ), ); await self.skipWaiting(); })(), ); }); self.addEventListener("activate", (event) => { event.waitUntil( (async () => { const keys = await caches.keys(); await Promise.all( keys.filter((key) => key !== CACHE_VERSION).map((key) => caches.delete(key)), ); await self.clients.claim(); })(), ); }); self.addEventListener("message", (event) => { if (event.data === "SKIP_WAITING") self.skipWaiting(); }); const isPlatformApi = (url) => url.pathname.startsWith("/_agent-native/") || url.pathname.startsWith("/api/") || url.pathname.startsWith("/.well-known/"); async function networkFirst(request, fallbackUrl) { const cache = await caches.open(CACHE_VERSION); try { const response = await fetch(request); if (response && response.ok) cache.put(request, response.clone()); return response; } catch { const cached = await cache.match(request); if (cached) return cached; if (fallbackUrl) { const fallback = await cache.match(fallbackUrl); if (fallback) return fallback; } return new Response("ออฟไลน์ — ไม่มีข้อมูลในแคช", { status: 503, headers: { "content-type": "text/plain; charset=utf-8" }, }); } } async function cacheFirst(request) { const cache = await caches.open(CACHE_VERSION); const cached = await cache.match(request); if (cached) { // อัปเดตพื้นหลัง (ไม่บล็อกการตอบ) fetch(request) .then((response) => { if (response && response.ok) cache.put(request, response.clone()); }) .catch(() => undefined); return cached; } const response = await fetch(request); if (response && response.ok) cache.put(request, response.clone()); return response; } self.addEventListener("fetch", (event) => { const { request } = event; if (request.method !== "GET") return; const url = new URL(request.url); if (url.origin !== self.location.origin) return; if (isPlatformApi(url)) return; // เอกสารหน้าเว็บ if (request.mode === "navigate") { event.respondWith(networkFirst(request, "/")); return; } // ข้อมูลจากไฟล์ JSON if (url.pathname.startsWith("/data/")) { event.respondWith(networkFirst(request)); return; } // ไฟล์นิ่ง if ( url.pathname.startsWith("/assets/") || /\.(?:css|js|mjs|svg|png|jpg|jpeg|webp|woff2?|ttf)$/.test(url.pathname) ) { event.respondWith(cacheFirst(request)); } });