From 2fa73f43d961b45791b87a75184883b06436d791 Mon Sep 17 00:00:00 2001 From: Bkk1234 Date: Fri, 23 Oct 2026 23:15:06 +0700 Subject: [PATCH] Add SEO foundation: robots, sitemap, JSON-LD, OG/Twitter cards, per-page metadata - src/lib/site.ts: single source for site name/url/description (rename via env only) - src/app/robots.ts: allow all, disallow /api /files, explicit Googlebot/Bingbot/DuckDuckBot/Bravebot/Applebot - src/app/sitemap.ts: static pages + routes/check/stops from backend, falls back to static when API is down - src/components/JsonLd.tsx: WebSite + Organization + SearchAction, BreadcrumbList, BusStop, Service - layout.tsx: metadataBase, canonical, keywords, robots, openGraph, twitter - generateMetadata for /stops/[code], /routes/[routeId], /check/[routeId] - canonical + description on /stops /routes /blog /about /credits - public/og-image.png 1200x630 --- frontend/public/locales/en.json | 2 +- frontend/public/locales/th.json | 2 +- frontend/src/app/about/page.tsx | 6 +- frontend/src/app/blog/page.tsx | 6 +- frontend/src/app/check/[routeId]/page.tsx | 53 ++++++- frontend/src/app/credits/page.tsx | 6 +- frontend/src/app/layout.tsx | 64 ++++++++- frontend/src/app/robots.ts | 24 ++++ frontend/src/app/routes/[routeId]/page.tsx | 57 +++++++- frontend/src/app/routes/page.tsx | 6 +- frontend/src/app/sitemap.ts | 69 +++++++++ frontend/src/app/stops/[code]/page.tsx | 59 +++++++- frontend/src/app/stops/page.tsx | 6 +- frontend/src/components/JsonLd.tsx | 148 ++++++++++++++++++++ frontend/src/components/views/CheckView.tsx | 8 ++ frontend/src/components/views/HomeView.tsx | 4 +- frontend/src/components/views/RouteView.tsx | 8 ++ frontend/src/components/views/StopView.tsx | 9 ++ frontend/src/data/locales/en.json | 2 +- frontend/src/data/locales/th.json | 2 +- frontend/src/lib/runtime.ts | 7 + frontend/src/lib/share.ts | 88 ++++++++++++ frontend/src/lib/site.ts | 51 +++++++ frontend/src/lib/ui/home.init.ts | 67 ++++----- frontend/src/lib/ui/share.init.ts | 141 +++++++++++++++++++ frontend/src/lib/ui/stop.init.ts | 21 ++- frontend/tsconfig.tsbuildinfo | 2 +- 27 files changed, 850 insertions(+), 68 deletions(-) create mode 100644 frontend/src/app/robots.ts create mode 100644 frontend/src/app/sitemap.ts create mode 100644 frontend/src/components/JsonLd.tsx create mode 100644 frontend/src/lib/share.ts create mode 100644 frontend/src/lib/site.ts create mode 100644 frontend/src/lib/ui/share.init.ts diff --git a/frontend/public/locales/en.json b/frontend/public/locales/en.json index 238d518..233ecce 100644 --- a/frontend/public/locales/en.json +++ b/frontend/public/locales/en.json @@ -89,7 +89,7 @@ "home.lead": "Browse routes, find stops, and preview a route right from this page", "home.chooseProvince": "Select Province", "home.chooseStation": "Select Terminal", - "share.label": "Share short link", + "share.label": "Share this page", "share.copied": "Link copied", "share.short": "Short link", "share.line": "Share on LINE", diff --git a/frontend/public/locales/th.json b/frontend/public/locales/th.json index 7f9638c..c98fecf 100644 --- a/frontend/public/locales/th.json +++ b/frontend/public/locales/th.json @@ -89,7 +89,7 @@ "home.lead": "ดูสายรถ, ค้นหาป้าย และเปิดตัวอย่างเส้นทางได้ทันทีจากหน้าแรก", "home.chooseProvince": "เลือกจังหวัด", "home.chooseStation": "เลือกสถานีขนส่งผู้โดยสาร", - "share.label": "แชร์ลิงก์สั้น", + "share.label": "แชร์หน้านี้", "share.copied": "คัดลอกลิงก์แล้ว", "share.short": "ลิงก์สั้น", "share.line": "แชร์ผ่าน LINE", diff --git a/frontend/src/app/about/page.tsx b/frontend/src/app/about/page.tsx index 1ca5791..81e61bc 100644 --- a/frontend/src/app/about/page.tsx +++ b/frontend/src/app/about/page.tsx @@ -1,7 +1,11 @@ import AboutView from "@/components/views/AboutView"; import PageRuntime from "@/components/PageRuntime"; -export const metadata = { title: "เกี่ยวกับเรา" }; +export const metadata = { + title: "เกี่ยวกับเรา", + description: "Atroute — แหล่งรวมข้อมูลรถโดยสารจังหวัดอ่างทอง", + alternates: { canonical: "/about" }, +}; export default function Page() { return ( diff --git a/frontend/src/app/blog/page.tsx b/frontend/src/app/blog/page.tsx index da2f142..d1fc32a 100644 --- a/frontend/src/app/blog/page.tsx +++ b/frontend/src/app/blog/page.tsx @@ -1,7 +1,11 @@ import BlogView from "@/components/views/BlogView"; import PageRuntime from "@/components/PageRuntime"; -export const metadata = { title: "บ่นไปเรื่อย" }; +export const metadata = { + title: "บ่นไปเรื่อย", + description: "บทความและเรื่องเล่าเกี่ยวกับการเดินทางด้วยรถโดยสารในจังหวัดอ่างทอง", + alternates: { canonical: "/blog" }, +}; export default function Page() { return ( diff --git a/frontend/src/app/check/[routeId]/page.tsx b/frontend/src/app/check/[routeId]/page.tsx index d4acf77..fd5a3f0 100644 --- a/frontend/src/app/check/[routeId]/page.tsx +++ b/frontend/src/app/check/[routeId]/page.tsx @@ -1,12 +1,61 @@ import CheckView from "@/components/views/CheckView"; import PageRuntime from "@/components/PageRuntime"; +import { BreadcrumbJsonLd, BusRouteJsonLd } from "@/components/JsonLd"; +import { SITE_NAME } from "@/lib/site"; +import { api } from "@/lib/api"; +import type { Metadata } from "next"; -export const metadata = { title: "เช็กเวลาเดินรถ" }; +type Params = { params: Promise<{ routeId: string }> }; -export default async function Page({ params }: { params: Promise<{ routeId: string }> }) { +async function loadRoute(id: string) { + try { + return await api.route(decodeURIComponent(id)); + } catch { + return null; + } +} + +export async function generateMetadata({ params }: Params): Promise { const { routeId } = await params; + const r = await loadRoute(routeId); + if (!r) { + return { title: "เช็กเวลาเดินรถ", alternates: { canonical: `/check/${routeId}` } }; + } + const title = `เวลาเดินรถ สาย ${r.route_id} ${r.name_th || ""}`.trim(); + const description = + `ตารางเวลาเดินรถสาย ${r.route_id} ${r.name_th || ""} ทั้งวันทำการและวันหยุด ` + + `พร้อมเวลารถออกจากแต่ละจุดจอด — ${SITE_NAME}`; + + return { + title, + description, + alternates: { canonical: `/check/${encodeURIComponent(r.route_id)}` }, + openGraph: { type: "article", title, description, url: `/check/${encodeURIComponent(r.route_id)}` }, + }; +} + +export default async function Page({ params }: Params) { + const { routeId } = await params; + const r = await loadRoute(routeId); + return ( <> + {r ? ( + <> + + + + ) : null} diff --git a/frontend/src/app/credits/page.tsx b/frontend/src/app/credits/page.tsx index fb7154c..af16ab7 100644 --- a/frontend/src/app/credits/page.tsx +++ b/frontend/src/app/credits/page.tsx @@ -1,6 +1,10 @@ import CreditsView from "@/components/views/CreditsView"; -export const metadata = { title: "เครดิต" }; +export const metadata = { + title: "เครดิต", + description: "แหล่งที่มาของข้อมูล รูปภาพ ฟอนต์ และซอฟต์แวร์ที่ใช้บนเว็บนี้", + alternates: { canonical: "/credits" }, +}; export default function Page() { return ;} diff --git a/frontend/src/app/layout.tsx b/frontend/src/app/layout.tsx index 2302f13..ff04dc4 100644 --- a/frontend/src/app/layout.tsx +++ b/frontend/src/app/layout.tsx @@ -1,4 +1,6 @@ import type { Metadata, Viewport } from "next"; +import { SITE_URL, SITE_NAME, SITE_TITLE, SITE_DESCRIPTION, SITE_LOCALE, SITE_OG_IMAGE } from "@/lib/site"; +import { SiteJsonLd } from "@/components/JsonLd"; import IconSprite from "@/components/IconSprite"; import PwaBoot from "@/components/PwaBoot"; import SiteBoot from "@/components/SiteBoot"; @@ -15,13 +17,64 @@ import "@/styles/responsive.css"; // (router เดิม inject