/** * /robots.txt — สร้างสด ๆ ที่ชั้น Nitro เพื่อให้บรรทัด `Sitemap:` ชี้โดเมนจริงของ request * (ไฟล์ static ใน public/ ต้องฮาร์ดโค้ดโดเมน จึงย้ายมาเป็น route) */ import { defineEventHandler, getRequestHeaders, getRequestProtocol, setResponseHeader, } from "h3"; import { originFromHeaders } from "../../app/lib/request-origin"; import { configuredOrigin } from "../../app/lib/site"; /** search engine ที่อนุญาตชัดเจน */ const ALLOWED_ENGINES = [ "Googlebot", "Bingbot", "DuckDuckBot", "Bravebot", "Applebot", ]; /** AI crawler ที่ไม่อนุญาต (ฝึกโมเดล/สรุปเนื้อหา) */ const AI_CRAWLERS = [ "GPTBot", "OAI-SearchBot", "ChatGPT-User", "ClaudeBot", "Claude-Web", "anthropic-ai", "Google-Extended", "Applebot-Extended", "PerplexityBot", "Perplexity-User", "CCBot", "Bytespider", "Amazonbot", "meta-externalagent", "cohere-ai", "Diffbot", "ImagesiftBot", "Omgilibot", "Timpibot", "VelenPublicWebCrawler", "YouBot", ]; /** ประกอบไฟล์ robots.txt จาก origin ของ request */ export function buildRobotsTxt(origin: string): string { const lines: string[] = [ "# robots.txt — AlterFlixHub", "# ค้นหาโรงหนังอิสระ/อาร์ตเฮาส์ในประเทศไทย", "", "# ── search engines (อนุญาตชัดเจน) ────────────────────────────────", ]; for (const engine of ALLOWED_ENGINES) { lines.push(`User-agent: ${engine}`, "Allow: /", ""); } lines.push( "# ── ค่าเริ่มต้น: เปิดให้ index แต่ปิดส่วนที่ไม่ควรอยู่ในผลค้นหา ──", "User-agent: *", "Allow: /", "Disallow: /api/", "Disallow: /admin", "Disallow: /admin/login", "Disallow: /_agent-native/", "Disallow: /c/ # ลิงก์ย่อสำหรับแชร์ (302 ไปหน้าโรงหนังจริง)", "", "# ── AI crawlers: ไม่อนุญาตให้นำเนื้อหาไปใช้ ─────────────────────", "# (รวมถึงการฝึกโมเดล/สรุปเนื้อหาของผู้ให้บริการ AI)", ); for (const crawler of AI_CRAWLERS) { lines.push(`User-agent: ${crawler}`, "Disallow: /", ""); } lines.push(`Sitemap: ${origin.replace(/\/+$/, "")}/sitemap.xml`, ""); return lines.join("\n"); } export default defineEventHandler((event) => { const origin = configuredOrigin() || originFromHeaders(getRequestHeaders(event), getRequestProtocol(event)); setResponseHeader(event, "Content-Type", "text/plain; charset=utf-8"); setResponseHeader(event, "Cache-Control", "public, max-age=3600"); return buildRobotsTxt(origin); });