- แถบกรองสองชั้นในแถวเดียว: ทั้งหมด → 3 ประเภท │ ยอดนิยม → หมวดภาพยนตร์เดิม
- เก็บประเภทที่ฟิลด์เดี่ยว cinema.venue_type (ค่าเดียว) + hasVenue()/filterCinemas({ venue })
- ย้าย "กลุ่มคนฉายหนัง" จาก categories ขึ้นชั้น 1 (ไม่ให้ปุ่มซ้ำ) · tag ยังอยู่และยังค้นหาเจอ
- เลือกได้ทีละชั้น: กดประเภทล้างหมวด / กดหมวดล้างประเภท / ทั้งหมดล้างทั้งคู่
- ปิด popularity path เมื่อกรองด้วยประเภท (กันจัดอันดับข้ามประเภท)
- หลังบ้าน: ช่อง "ประเภทโรงหนัง" ในหมวดข้อมูลหลัก + sanitize เฉพาะค่าใน VENUE_TYPES
- i18n: messages.venues (TH/EN) · override ได้ผ่าน site.terms
- scripts/set-venue-types.mjs จัดประเภท 29 โรง (15 ท้องถิ่น / 11 อินดี้ / 3 กลุ่มคนฉายหนัง) พร้อมสำรอง content.json
- tests: hasVenue + filterCinemas (165 ผ่าน) · sw v2.0.5 · DESIGN/AGENTS/README/admin-guide/api อัปเดต
150 lines
5.9 KiB
JavaScript
150 lines
5.9 KiB
JavaScript
/*
|
|
* 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.0.5";
|
|
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();
|
|
}
|
|
})(),
|
|
);
|
|
}
|
|
});
|