refactor(media): แยกรูปออกจาก code — รูปทั้งหมดอยู่บน object storage
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 ได้
This commit is contained in:
1 parent
bc0e7a963e
commit
ddfb0a5053
67 files changed
+442
-104
No files matched your search
@@ -16,9 +16,10 @@
|
||||
* | /assets/* · รูป · ฟอนต์ (.png/.svg…) | cache-first + อัปเดตเบื้องหลัง |
|
||||
* | /api/* · /__* · /sw.js | ไม่จัดการ (ปล่อยผ่านไปเครือข่าย) |
|
||||
* | โดเมนอื่น (รูป hotlink) | ไม่จัดการ |
|
||||
* | รูปบน storage.hobbypj.com | cache-first (แคชเดียวกับ assets) |
|
||||
* | /app/* · /node_modules/* · /@* | ไม่จัดการ (โมดูลของ dev server) |
|
||||
*/
|
||||
const VERSION = "v2.1.5";
|
||||
const VERSION = "v2.1.6";
|
||||
const PAGES_CACHE = `alterflixhub-pages-${VERSION}`;
|
||||
const ASSETS_CACHE = `alterflixhub-assets-${VERSION}`;
|
||||
|
||||
@@ -68,6 +69,39 @@ 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 (
|
||||
@@ -93,7 +127,11 @@ self.addEventListener("fetch", (event) => {
|
||||
} catch {
|
||||
return;
|
||||
}
|
||||
if (!isSameOrigin(url)) 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;
|
||||
|
||||
Reference in new issue
Block a user