diff --git a/app/components/HotDocsPreview.tsx b/app/components/HotDocsPreview.tsx index 206c0b9..f50a663 100644 --- a/app/components/HotDocsPreview.tsx +++ b/app/components/HotDocsPreview.tsx @@ -2,7 +2,8 @@ import { useEffect, useState } from "react"; import Link from "next/link"; -import { useTranslations } from "next-intl"; +import { useLocale, useTranslations } from "next-intl"; +import { topDocHref } from "@/lib/top-doc-href"; interface TopDocDto { path: string; @@ -56,6 +57,7 @@ export function HotDocsPreviewSkeleton() { */ export function HotDocsPreview() { const t = useTranslations("hotDocs"); + const locale = useLocale(); const [docs, setDocs] = useState(null); useEffect(() => { @@ -114,7 +116,7 @@ export function HotDocsPreview() {
next, initialWindow, @@ -165,7 +168,7 @@ export function HotDocsTab({ initialWindow }: { initialWindow: WindowParam }) { {state.docs.map((doc, idx) => (
diff --git a/lib/top-doc-href.ts b/lib/top-doc-href.ts new file mode 100644 index 0000000..6f5e2c2 --- /dev/null +++ b/lib/top-doc-href.ts @@ -0,0 +1,11 @@ +/** + * top-docs(热门文档榜)后端返回的 path 是无 locale 的 canonical(形如 /docs/learn/ai)。 + * 拼上当前 locale 前缀得到可点击 URL。 + * + * 防御性归一化:剥掉可能残留的 content/ 前缀和多余前导斜杠,避免后端某次回退到 + * 旧格式(content/docs/...)时前端直接拼出 /en/content/docs/... 死链。 + */ +export function topDocHref(locale: string, path: string): string { + const clean = "/" + path.replace(/^\/+/, "").replace(/^content\//, ""); + return `/${locale}${clean}`; +}