code อยู่ใน git · รูปอยู่บน SeaweedFS (https://storage.hobbypj.com/hobbypj/alterflixhub/) - อัปโหลด images/ (21 ไฟล์) + og/ (1 ไฟล์) ขึ้น storage แล้วตรวจ sha256 ตรงทุกไฟล์ - แทนที่อ้างอิง 894 จุด ใน 37 ไฟล์: /images/… → <storage>/images/…, /og/… → <storage>/og/… (absolute URL ทุกที่ รวม JSON-LD/og:image/twitter:image และ placeholder ในหลังบ้าน) - ลบ images/ + og/ ออกจาก repo · สำเนาในเครื่องย้ายไป media/ (gitignored) - scripts/media-to-storage.py: สคริปต์ที่ใช้ย้าย (บันทึกวิธี + ใช้ซ้ำได้เมื่อเปลี่ยน base) - scripts/media-sync.py: --check / --upload รูปขึ้น storage (basic auth หรือ SSH tunnel ไป filer) - sw.js: bump v2.1.6 + แคชรูปจาก storage แบบ cache-first (isMedia/mediaCacheFirst) กันออฟไลน์แล้วรูปหาย - docker/traefik-storage-alterflixhub.yml: router เปิดอ่านสาธารณะเฉพาะ /hobbypj/alterflixhub/ (host storage บังคับ Basic auth ทั้ง host — ต้องมี router priority 300 + CORS ก่อน) - docs/media-storage.md: วิธีอัปโหลด/เพิ่มรูป + ข้อกำหนดฝั่ง Traefik - .gitignore/.dockerignore: กัน media/, images/, og/ กลับเข้า repo/image ไอคอน PWA (icon-*.png, favicon.svg) ยังอยู่ใน repo — ต้อง same-origin ให้ติดตั้ง PWA ได้
189 lines
7.4 KiB
JavaScript
189 lines
7.4 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) | ไม่จัดการ |
|
|
* | รูปบน storage.hobbypj.com | cache-first (แคชเดียวกับ assets) |
|
|
* | /app/* · /node_modules/* · /@* | ไม่จัดการ (โมดูลของ dev server) |
|
|
*/
|
|
const VERSION = "v2.1.6";
|
|
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",
|
|
"/assets/card-click.v2.js",
|
|
];
|
|
|
|
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;
|
|
}
|
|
|
|
/* รูปอยู่บน object storage (SeaweedFS) — host เดียว เปิด public read เฉพาะ prefix นี้
|
|
* ต้องแคชด้วย ไม่งั้นออฟไลน์แล้วรูปหายทั้งเว็บ (ไฟล์ที่เหลือยังอยู่ใน image) */
|
|
const MEDIA_HOST = "storage.hobbypj.com";
|
|
const MEDIA_PREFIX = "/hobbypj/alterflixhub/";
|
|
|
|
function isMedia(url) {
|
|
return (
|
|
url.hostname === MEDIA_HOST &&
|
|
url.pathname.startsWith(MEDIA_PREFIX) &&
|
|
/\.(?:png|jpe?g|webp|avif|gif|svg)$/i.test(url.pathname)
|
|
);
|
|
}
|
|
|
|
async function mediaCacheFirst(request) {
|
|
const cache = await caches.open(ASSETS_CACHE);
|
|
const cached = await cache.match(request);
|
|
if (cached) {
|
|
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();
|
|
}
|
|
}
|
|
|
|
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)) {
|
|
// รูปจาก object storage — แคช cache-first (opaque/cors ก็เก็บได้)
|
|
if (isMedia(url)) event.respondWith(mediaCacheFirst(request));
|
|
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();
|
|
}
|
|
})(),
|
|
);
|
|
}
|
|
});
|