/* * Service worker ของ AlterFlixHub * * กลยุทธ์ * - หน้าเว็บ (navigate): network-first → ออฟไลน์ค่อยใช้แคชของหน้านั้น → สุดท้าย /offline.html * - ไฟล์ static (/assets/*.js|css, รูป, ฟอนต์): cache-first แล้วอัปเดตแคชเบื้องหลัง * - /api/* และของภายนอกโดเมน: ไม่แตะ (ข้อมูลต้องสด + เคารพความเป็นส่วนตัว) * - activate: ลบแคชเวอร์ชันเก่าทั้งหมด (กันเคสค้างเวอร์ชัน — เคยมีแคช `alterflixhub-v1.2.0` ค้าง) * * เวลาปล่อยเวอร์ชันใหม่: เปลี่ยน VERSION — พอ SW ตัวใหม่ activate จะเก็บกวาดแคชเก่าให้เอง * * ตารางกลยุทธ์ (อ่านประกอบ fetch handler ด้านล่าง) * | คำขอ | กลยุทธ์ | * |-------------------------------------|------------------------------------------| * | navigate (เปิดหน้า) | network-first → แคชของหน้านั้น → /offline.html | * | /assets/* · รูป · ฟอนต์ (.png/.svg…) | cache-first + อัปเดตเบื้องหลัง | * | /api/* · /__* · /sw.js | ไม่จัดการ (ปล่อยผ่านไปเครือข่าย) | * | โดเมนอื่น (รูป hotlink) | ไม่จัดการ | * | /app/* · /node_modules/* · /@* | ไม่จัดการ (โมดูลของ dev server) | */ const VERSION = "v2.1.0"; const PAGES_CACHE = `alterflixhub-pages-${VERSION}`; const ASSETS_CACHE = `alterflixhub-assets-${VERSION}`; /** ไฟล์ที่ต้องมีติดเครื่องไว้ตั้งแต่ก่อนออฟไลน์ครั้งแรก */ const PRECACHE = [ "/offline.html", "/manifest.json", "/icon-192.png", "/icon-512.png", "/favicon.svg", ]; self.addEventListener("install", (event) => { event.waitUntil( (async () => { const cache = await caches.open(ASSETS_CACHE); // ไฟล์ใดโหลดไม่ได้ (เช่นตอน dev ที่ยังไม่ build) ไม่ทำให้ install ล้มทั้งชุด await Promise.all( PRECACHE.map((url) => cache.add(url).catch(() => undefined)), ); })(), ); }); self.addEventListener("activate", (event) => { event.waitUntil( (async () => { const keep = new Set([PAGES_CACHE, ASSETS_CACHE]); const names = await caches.keys(); await Promise.all( names .filter((name) => !keep.has(name)) .map((name) => caches.delete(name)), ); // เข้าคุมแท็บที่เปิดอยู่ทันที เพื่อไม่ให้ค้าง SW เวอร์ชันเก่า await self.clients.claim(); })(), ); }); self.addEventListener("message", (event) => { if (event.data === "SKIP_WAITING") self.skipWaiting(); }); function isSameOrigin(url) { return url.origin === self.location.origin; } function isStaticAsset(url) { // ไม่แคชเส้นทางของ bundler/dev server (Vite เสิร์ฟโมดูลที่ /app, /node_modules, /@…) if ( url.pathname.startsWith("/app/") || url.pathname.startsWith("/node_modules/") || url.pathname.startsWith("/@") ) { return false; } if (url.pathname.startsWith("/assets/")) return true; return /\.(?:js|mjs|css|woff2?|ttf|png|jpe?g|webp|avif|gif|svg|ico)$/i.test( url.pathname, ); } self.addEventListener("fetch", (event) => { const request = event.request; if (request.method !== "GET") return; let url; try { url = new URL(request.url); } catch { return; } if (!isSameOrigin(url)) return; // ข้อมูลสด/ข้อมูลหลังบ้าน — ห้ามแคช if (url.pathname.startsWith("/api/") || url.pathname.startsWith("/__")) return; if (url.pathname === "/sw.js") return; if (request.mode === "navigate") { event.respondWith( (async () => { try { const fresh = await fetch(request); if (fresh && fresh.ok) { const cache = await caches.open(PAGES_CACHE); cache.put(request, fresh.clone()).catch(() => undefined); } return fresh; } catch { const cached = await caches.match(request); if (cached) return cached; const offline = await caches.match("/offline.html"); return offline || Response.error(); } })(), ); return; } if (isStaticAsset(url)) { event.respondWith( (async () => { const cache = await caches.open(ASSETS_CACHE); const cached = await cache.match(request); if (cached) { // อัปเดตเบื้องหลัง (stale-while-revalidate) — ไม่หน่วงการแสดงผล fetch(request) .then((fresh) => { if (fresh && fresh.ok) cache.put(request, fresh).catch(() => undefined); }) .catch(() => undefined); return cached; } try { const fresh = await fetch(request); if (fresh && fresh.ok) { cache.put(request, fresh.clone()).catch(() => undefined); } return fresh; } catch { return Response.error(); } })(), ); } });