Files
web-alterflixhub/app/lib/seo.ts
T
Bkk1234 319c271d23 feat: บทความ 2 หน้า + แชร์/ลิงก์ย่อ + PWA + แอนิเมชัน + SEO/AI + เอกสาร
- บทความจากหลังบ้าน: /for-film-lovers, /for-cinemas (ArticlePage + ArticleEditor แบบบล็อกย่อหน้า/รูป)
- แชร์: ปุ่มบนการ์ดและท้ายหน้ารายละเอียด · ลิงก์ย่อ /c/<slug> (302) · ธีมแก้วตาม DESIGN.md
- PWA: manifest/sw/offline + ถอดการล็อกซูม (WCAG 1.4.4) + touch target ≥44px
- แอนิเมชัน reveal: IntersectionObserver + gate ด้วย html.fx-motion · เคารพ prefers-reduced-motion
- ค้นหา: normalize/ตัดวรรณยุกต์ + AND หลายคำ + จัดอันดับ · รองรับ /?q= จาก SearchAction
- SEO: pageMeta (canonical/OG/Twitter) + JSON-LD (WebSite/Organization/BreadcrumbList/MovieTheater/Article)
- ไม่ฝังโดเมนในโค้ด: origin มาจาก request (x-forwarded-host/proto) ทั้ง canonical, sitemap, robots, ลิงก์แชร์
- กัน AI: robots.txt (Nitro route, บล็อก AI 21 ตัว) + ai.txt + OG image 1200x630
- เอกสาร: README สรุปโปรเจกต์, DESIGN.md (motion/PWA/SEO/ข้อมูล), architecture.html, admin-guide
- tests: 135 ผ่าน (16 ไฟล์)
2026-11-10 02:31:10 +07:00

212 lines
7.2 KiB
TypeScript

/**
* ตัวช่วยสร้าง meta / JSON-LD ให้ทุก route (SEO พื้นฐาน)
*
* ทุกฟังก์ชันคืน "meta descriptor" ของ React Router — ใช้ได้ทั้ง SSR และตอนสลับหน้า
* ค่าที่ crawler เห็นจึงเหมือนกันทุกครั้ง (ไม่พึ่ง JavaScript ฝั่ง client)
*/
import type { MetaDescriptor } from "react-router";
import { SITE } from "@/lib/site";
/** เติมโดเมนให้พาธ — canonical/OG ต้องเป็น URL เต็มเสมอ */
export function absoluteUrl(origin: string, path: string): string {
const suffix = path.startsWith("/") ? path : `/${path}`;
return `${origin.replace(/\/+$/, "")}${suffix}`;
}
type MetaMatch = { id?: string; data?: unknown };
/** อ่าน origin ที่ root loader คำนวณไว้ (ใช้ใน meta() ของ route ลูก) */
export function originFromMatches(
matches: Array<MetaMatch> | undefined,
): string {
if (!matches) return "";
// ไม่ยึด id ของ root — เอา match แรกที่มี siteUrl (framework เปลี่ยน id ได้)
for (const match of matches) {
const data = match.data as { siteUrl?: unknown } | undefined;
if (typeof data?.siteUrl === "string" && data.siteUrl !== "")
return data.siteUrl;
}
return "";
}
type PageMetaInput = {
/** origin ของคำขอ เช่น `https://alterflixhub.hobbypj.com` (ห้ามฮาร์ดโค้ด) */
origin: string;
title: string;
description: string;
/** พาธของหน้านั้น ๆ เช่น `/about` หรือ `/cinema/lido-connect` */
path: string;
/** og:type — `article` สำหรับหน้าบทความ, `website` สำหรับหน้าทั่วไป */
type?: "website" | "article";
/** true = ไม่ให้ index (เช่นหน้าหลังบ้าน) */
noIndex?: boolean;
};
/** meta ชุดมาตรฐานของหนึ่งหน้า: title, description, canonical, OG, Twitter */
export function pageMeta({
origin,
title,
description,
path,
type = "website",
noIndex = false,
}: PageMetaInput): MetaDescriptor[] {
const url = absoluteUrl(origin, path);
const image = absoluteUrl(origin, SITE.ogImage);
return [
{ title },
{ name: "description", content: description },
{
name: "robots",
content: noIndex
? "noindex, nofollow"
: "index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1",
},
{ tagName: "link", rel: "canonical", href: url },
{ property: "og:type", content: type },
{ property: "og:site_name", content: SITE.name },
{ property: "og:locale", content: SITE.locale },
{ property: "og:title", content: title },
{ property: "og:description", content: description },
{ property: "og:url", content: url },
{ property: "og:image", content: image },
{ property: "og:image:width", content: "1200" },
{ property: "og:image:height", content: "630" },
{ property: "og:image:alt", content: SITE.name },
{ name: "twitter:card", content: "summary_large_image" },
{ name: "twitter:title", content: title },
{ name: "twitter:description", content: description },
{ name: "twitter:image", content: image },
];
}
/** JSON-LD ของเว็บไซต์ + องค์กร (ใส่ที่หน้าแรกครั้งเดียว) */
export function websiteJsonLd(origin: string): MetaDescriptor[] {
return [
{
"script:ld+json": {
"@context": "https://schema.org",
"@graph": [
{
"@type": "WebSite",
"@id": `${origin}/#website`,
url: `${origin}/`,
name: SITE.name,
description: SITE.description,
inLanguage: "th-TH",
publisher: { "@id": `${origin}/#organization` },
potentialAction: {
"@type": "SearchAction",
target: `${origin}/?q={search_term_string}`,
"query-input": "required name=search_term_string",
},
},
{
"@type": "Organization",
"@id": `${origin}/#organization`,
name: SITE.name,
url: `${origin}/`,
logo: absoluteUrl(origin, "/icon-512.png"),
},
],
},
},
];
}
/** JSON-LD breadcrumb ของหน้า (ช่วยให้ Google แสดงเส้นทางในผลค้นหา) */
export function breadcrumbJsonLd(
origin: string,
items: Array<{ name: string; path: string }>,
): MetaDescriptor[] {
return [
{
"script:ld+json": {
"@context": "https://schema.org",
"@type": "BreadcrumbList",
itemListElement: items.map((item, index) => ({
"@type": "ListItem",
position: index + 1,
name: item.name,
item: absoluteUrl(origin, item.path),
})),
},
},
];
}
/** JSON-LD ของหน้าโรงหนัง — MovieTheater + ที่ตั้ง + พิกัด */
export function cinemaJsonLd(input: {
origin: string;
name: string;
description: string;
path: string;
image?: string | null;
coordinates?: { latitude: number; longitude: number } | null;
locationLabel?: string;
mapUrl?: string | null;
}): MetaDescriptor[] {
const image = input.image && input.image.trim() !== "" ? input.image : null;
return [
{
"script:ld+json": {
"@context": "https://schema.org",
"@type": "MovieTheater",
name: input.name,
description: input.description,
url: absoluteUrl(input.origin, input.path),
...(image
? {
image: image.startsWith("http")
? image
: absoluteUrl(input.origin, image),
}
: {}),
...(input.coordinates
? {
geo: {
"@type": "GeoCoordinates",
latitude: input.coordinates.latitude,
longitude: input.coordinates.longitude,
},
}
: {}),
address: {
"@type": "PostalAddress",
addressCountry: "TH",
...(input.locationLabel
? { streetAddress: input.locationLabel }
: {}),
},
...(input.mapUrl ? { hasMap: input.mapUrl } : {}),
},
},
];
}
/** JSON-LD ของหน้าบทความ (สำหรับโรงหนัง / สำหรับคนรักหนัง) */
export function articleJsonLd(input: {
origin: string;
headline: string;
description: string;
path: string;
}): MetaDescriptor[] {
return [
{
"script:ld+json": {
"@context": "https://schema.org",
"@type": "Article",
headline: input.headline,
description: input.description,
inLanguage: "th-TH",
mainEntityOfPage: absoluteUrl(input.origin, input.path),
author: { "@type": "Organization", name: SITE.name },
publisher: { "@type": "Organization", name: SITE.name },
},
},
];
}