From 1c38d8e286c0beeb47d51c825bdd8800bdce66ee Mon Sep 17 00:00:00 2001 From: Bkk1234 Date: Sat, 21 Nov 2026 15:10:13 +0700 Subject: [PATCH] =?UTF-8?q?feat(build):=20static=20export=20(index.html=20?= =?UTF-8?q?=E0=B8=95=E0=B9=88=E0=B8=AD=E0=B8=AB=E0=B8=99=E0=B9=89=E0=B8=B2?= =?UTF-8?q?)=20=E0=B8=AA=E0=B8=B3=E0=B8=AB=E0=B8=A3=E0=B8=B1=E0=B8=9A?= =?UTF-8?q?=E0=B9=82=E0=B8=AE=E0=B8=AA=E0=B8=95=E0=B9=8C=E0=B9=81=E0=B8=9A?= =?UTF-8?q?=E0=B8=9A=E0=B9=84=E0=B8=9F=E0=B8=A5=E0=B9=8C=E0=B8=99=E0=B8=B4?= =?UTF-8?q?=E0=B9=88=E0=B8=87?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - scripts/static-export.mjs: ดึง HTML ทุกหน้าจาก production server มาเป็นไฟล์นิ่ง · index.html, about/, for-cinemas/, for-film-lovers/, cinema// (ทุกโรงที่ไม่ hidden) · c// = หน้า redirect แบบ meta refresh + JS · คัดลอก .output/public (assets, รูป, manifest, sw, sitemap, robots) + 404.html · อ่าน slug จาก data/content/content.json (การ์ดหน้าแรก render ฝั่ง client จึงไม่มี link ใน HTML) · inject data-static-export-nav: ดักคลิก link ภายในเป็น full page load (static host ไม่มี endpoint ?_data= ของ React Router) - package.json: script "export:static" · .gitignore: static-export/ - README: วิธี build/export/ทดสอบ + ข้อจำกัด (ไม่มี /admin, /api, 302, meta อบตอน export) --- .gitignore | 2 + README.md | 20 ++++++ package.json | 1 + scripts/static-export.mjs | 146 ++++++++++++++++++++++++++++++++++++++ 4 files changed, 169 insertions(+) create mode 100644 scripts/static-export.mjs diff --git a/.gitignore b/.gitignore index acf4309..45f2180 100644 --- a/.gitignore +++ b/.gitignore @@ -41,3 +41,5 @@ learnings.md .vercel/ .agent-native/ data/*.lock +# static export (artifact) +static-export/ diff --git a/README.md b/README.md index 16abbef..6ca5e41 100644 --- a/README.md +++ b/README.md @@ -94,6 +94,26 @@ AGENTS.md กฎสำหรับ agent/AI ที่แก้โค้ | `ANALYTICS_SALT` | แนะนำ | เกลือ hash ผู้เข้าชม (กันคาดเดา) | | `SITE_URL` / `SHARE_ORIGIN` | ไม่ต้อง | บังคับโดเมนเฉพาะกรณี (ปกติปล่อยว่าง) | +## Static export (โฮสต์แบบไฟล์นิ่ง / มี `index.html`) + +แอปนี้เป็น SSR + หลังบ้านเขียนไฟล์ได้ → ค่าปกติต้องรันเป็น Node server (`node .output/server/index.mjs`) +ถ้าต้องการ **ไฟล์นิ่งที่มี `index.html`** ใช้สคริปต์ export (ดึง HTML ทุกหน้าจากตัว server ที่รันอยู่): + +```bash +pnpm build # ได้ .output (preset node) +PORT=8090 node .output/server/index.mjs # เปิด server ตัวจริง +pnpm export:static http://127.0.0.1:8090 static-export +python -m http.server 8091 --directory static-export # ทดสอบแบบ static +``` + +ผลลัพธ์: `static-export/index.html`, `about/index.html`, `cinema//index.html` (ทุกโรงที่ไม่ `hidden`), `c//index.html` (redirect), `sitemap.xml`, `robots.txt`, `assets/`, `images/`, `404.html` + +**ข้อจำกัดที่ตั้งใจ (static ไม่มี backend)** +- ไม่มี `/admin` และ `/api/*` — ต้องใช้ Node server ถ้าจะแก้เนื้อห ผมเพิ่ม/แก้ผ่านหลังบ้าน +- ไม่มี `?_data=` ของ React Router → สคริปต์ inject ตัวดักคลิก (`data-static-export-nav`) ให้ทุก link เป็นการโหลดหน้าเต็ม (ทุกหน้ามี HTML ของตัวเอง) — คลิก/ย้อนกลับ/รีเฟรชทำงานปกติ แต่ไม่มีการเปลี่ยนหน้าแบบ SPA +- ลิงก์แชร์ `/c/` กลายเป็นหน้า `meta refresh` + JS redirect (ไม่ใช่ 302 ของ server) +- canonical/OG ถูกอบตอน export — ตั้ง `SITE_URL` ตอนรัน server ก่อน export ถ้าต้องการโดเมนจริงใน meta + ## เอกสารอื่น - [DESIGN.md](DESIGN.md) — สัญญาการออกแบบ: พาเลตต์ ฟอนต์ โลโก้ layout motion PWA SEO และกติกากันข้อมูลหาย diff --git a/package.json b/package.json index cb905ed..fcf8434 100644 --- a/package.json +++ b/package.json @@ -8,6 +8,7 @@ "migrate:production": "tsx scripts/migrate-production.ts && pnpm db:migrate", "dev": "agent-native dev", "build": "agent-native build", + "export:static": "node scripts/static-export.mjs", "start": "agent-native start", "typecheck": "agent-native typecheck", "action": "agent-native action", diff --git a/scripts/static-export.mjs b/scripts/static-export.mjs new file mode 100644 index 0000000..244aebb --- /dev/null +++ b/scripts/static-export.mjs @@ -0,0 +1,146 @@ +/** + * static-export — ดึง HTML ทุกหน้าจาก production server มาเป็นไฟล์ static (มี index.html) + * + * ใช้: node scripts/static-export.mjs [baseUrl] [outDir] + * baseUrl default http://127.0.0.1:8090 + * outDir default ./static-export + * + * หมายเหตุสำคัญ: ไฟล์ HTML ที่ได้ "hydrate ได้" (ฟิลเตอร์/ค้นหา/แสดงเพิ่ม ทำงาน) + * แต่การคลิก link ไปหน้าอื่นจะเป็นการโหลดหน้าใหม่แบบเต็ม (ไม่มี `?_data=` บน static host) + */ +import fs from "node:fs"; +import path from "node:path"; + +const BASE = (process.argv[2] ?? "http://127.0.0.1:8090").replace(/\/$/, ""); +const OUT = process.argv[3] ?? "static-export"; + +const STATIC_ROUTES = [ + "/", + "/about", + "/for-cinemas", + "/for-film-lovers", + "/sitemap.xml", + "/robots.txt", +]; + +async function get(urlPath) { + const res = await fetch(`${BASE}${urlPath}`, { redirect: "manual" }); + const body = await res.text(); + return { + status: res.status, + type: res.headers.get("content-type") ?? "", + body, + }; +} + +function writeRoute(routePath, body) { + const clean = routePath.split("?")[0].replace(/\/$/, ""); + const isFile = /\.[a-z0-9]+$/i.test(clean); + const target = isFile + ? path.join(OUT, clean) + : path.join(OUT, clean, "index.html"); + fs.mkdirSync(path.dirname(target), { recursive: true }); + fs.writeFileSync(target, body); + return target; +} + +// 1) อ่าน slug จาก data/content/content.json (การ์ดในหน้าแรก render ฝั่ง client จึงหา link ใน HTML ไม่เจอ) +const contentPath = path.join("data", "content", "content.json"); +const home = await get("/"); +if (home.status !== 200) { + console.error(`หน้าแรกตอบ ${home.status} — server รันอยู่ไหม? (${BASE})`); + process.exit(1); +} +fs.rmSync(OUT, { recursive: true, force: true }); +fs.mkdirSync(OUT, { recursive: true }); + +let slugs = []; +if (fs.existsSync(contentPath)) { + const content = JSON.parse(fs.readFileSync(contentPath, "utf8")); + slugs = (content.cinemas ?? []) + .filter((cinema) => cinema.hidden !== true && cinema.slug) + .map((cinema) => cinema.slug); +} else { + slugs = [...home.body.matchAll(/\/cinema\/([a-z0-9-]+)/g)].map((m) => m[1]); +} +const uniqueSlugs = [...new Set(slugs)]; + +const routes = [ + ...STATIC_ROUTES, + ...uniqueSlugs.map((slug) => `/cinema/${slug}`), +]; + +let ok = 0; +const failed = []; +for (const route of routes) { + const res = await get(route); + if (res.status !== 200) { + failed.push(`${route} (${res.status})`); + continue; + } + writeRoute(route, res.body); + ok++; +} + +// 2.5) static host ไม่มี endpoint `?_data=` ของ React Router +// → ดักคลิก link ภายในให้เป็นการโหลดหน้าเต็ม (ทุกหน้ามี HTML ของตัวเองอยู่แล้ว) +const STATIC_NAV_PATCH = ``; + +function injectNavPatch(file) { + const html = fs.readFileSync(file, "utf8"); + if (html.includes("data-static-export-nav")) return; + const patched = html.includes("") + ? html.replace("", `${STATIC_NAV_PATCH}`) + : html + STATIC_NAV_PATCH; + fs.writeFileSync(file, patched); +} + +// 2) คัดลอก asset ที่ build ไว้แล้ว (ไม่ต้องโหลดซ้ำ) +const publicDir = path.join(".output", "public"); +if (fs.existsSync(publicDir)) { + fs.cpSync(publicDir, OUT, { recursive: true }); +} + +// 3) ลิงก์ย่อ /c/ ทำเป็นหน้า redirect แบบ static +let redirects = 0; +for (const slug of uniqueSlugs) { + const dir = path.join(OUT, "c", slug); + fs.mkdirSync(dir, { recursive: true }); + fs.writeFileSync( + path.join(dir, "index.html"), + `กำลังพาไป… + + + +ไปที่หน้าโรงหนัง +`, + ); + redirects++; +} + +// 4) 404 fallback (static host ส่วนใหญ่ใช้ 404.html) +fs.writeFileSync( + path.join(OUT, "404.html"), + fs.readFileSync(path.join(OUT, "index.html"), "utf8"), +); + +for (const file of fs.readdirSync(OUT, { recursive: true })) { + const full = path.join(OUT, String(file)); + if (full.endsWith(".html")) injectNavPatch(full); +} + +const files = fs.readdirSync(OUT, { recursive: true }).length; +console.log( + `export เสร็จ: ${ok}/${routes.length} หน้า · ${redirects} redirect · ${files} entries`, +); +console.log(`outDir: ${path.resolve(OUT)}`); +if (failed.length) console.log(`ล้มเหลว: ${failed.join(", ")}`);