Skip to content

Commit edbb4c2

Browse files
longsizhuogithub-actions[bot]claude
authored
fix(rank): 热门文档榜链接补 locale 前缀,直达 canonical 不再 404 (#362)
后端 top-docs 返回的 path 是无 locale 的 canonical(/docs/...)。HotDocsTab / HotDocsPreview 之前直接 <Link href={doc.path}>,配合后端旧 bug 渲染出 /en/content/docs/... 死链。 新增 lib/top-doc-href.ts:拼当前 locale 前缀 + 防御性剥掉残留 content/ 前缀, 两个组件共用。即使后端某次回退到旧格式也不会再拼出 404。 Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com> Co-authored-by: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
1 parent 09840bf commit edbb4c2

3 files changed

Lines changed: 19 additions & 3 deletions

File tree

‎app/components/HotDocsPreview.tsx‎

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,8 @@
22

33
import { useEffect, useState } from "react";
44
import Link from "next/link";
5-
import { useTranslations } from "next-intl";
5+
import { useLocale, useTranslations } from "next-intl";
6+
import { topDocHref } from "@/lib/top-doc-href";
67

78
interface TopDocDto {
89
path: string;
@@ -56,6 +57,7 @@ export function HotDocsPreviewSkeleton() {
5657
*/
5758
export function HotDocsPreview() {
5859
const t = useTranslations("hotDocs");
60+
const locale = useLocale();
5961
const [docs, setDocs] = useState<TopDocDto[] | null>(null);
6062

6163
useEffect(() => {
@@ -114,7 +116,7 @@ export function HotDocsPreview() {
114116
</span>
115117
<div className="flex-1 min-w-0">
116118
<Link
117-
href={doc.path}
119+
href={topDocHref(locale, doc.path)}
118120
className="font-serif text-sm font-bold uppercase text-[var(--foreground)] hover:text-[#CC0000] transition-colors leading-tight line-clamp-2 block"
119121
data-umami-event="navigation_click"
120122
data-umami-event-region="hot_docs_preview"

‎app/components/rank/HotDocsTab.tsx‎

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
import { useReducer, useEffect } from "react";
44
import Link from "next/link";
5+
import { useLocale } from "next-intl";
6+
import { topDocHref } from "@/lib/top-doc-href";
57

68
type HotDoc = {
79
path: string;
@@ -38,6 +40,7 @@ function reducer(_: State, action: Action): State {
3840
// 从根拔掉逃生门,同源代理永远成立;想 curl 测后端就直连 api 域名。
3941

4042
export function HotDocsTab({ initialWindow }: { initialWindow: WindowParam }) {
43+
const locale = useLocale();
4144
const [windowParam, setWindowParam] = useReducer(
4245
(_: WindowParam, next: WindowParam) => next,
4346
initialWindow,
@@ -165,7 +168,7 @@ export function HotDocsTab({ initialWindow }: { initialWindow: WindowParam }) {
165168
{state.docs.map((doc, idx) => (
166169
<Link
167170
key={doc.path}
168-
href={doc.path}
171+
href={topDocHref(locale, doc.path)}
169172
className="group w-full flex items-center gap-4 border border-[var(--foreground)] p-4 bg-[var(--background)] hard-shadow-hover transition-all"
170173
>
171174
<div className="font-mono text-2xl font-bold w-12 text-center text-[var(--foreground)] shrink-0">

‎lib/top-doc-href.ts‎

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
/**
2+
* top-docs(热门文档榜)后端返回的 path 是无 locale 的 canonical(形如 /docs/learn/ai)。
3+
* 拼上当前 locale 前缀得到可点击 URL。
4+
*
5+
* 防御性归一化:剥掉可能残留的 content/ 前缀和多余前导斜杠,避免后端某次回退到
6+
* 旧格式(content/docs/...)时前端直接拼出 /en/content/docs/... 死链。
7+
*/
8+
export function topDocHref(locale: string, path: string): string {
9+
const clean = "/" + path.replace(/^\/+/, "").replace(/^content\//, "");
10+
return `/${locale}${clean}`;
11+
}

0 commit comments

Comments
 (0)