File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change 22
33import { useEffect , useState } from "react" ;
44import 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
78interface TopDocDto {
89 path : string ;
@@ -56,6 +57,7 @@ export function HotDocsPreviewSkeleton() {
5657 */
5758export 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"
Original file line number Diff line number Diff line change 22
33import { useReducer , useEffect } from "react" ;
44import Link from "next/link" ;
5+ import { useLocale } from "next-intl" ;
6+ import { topDocHref } from "@/lib/top-doc-href" ;
57
68type HotDoc = {
79 path : string ;
@@ -38,6 +40,7 @@ function reducer(_: State, action: Action): State {
3840// 从根拔掉逃生门,同源代理永远成立;想 curl 测后端就直连 api 域名。
3941
4042export 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" >
Original file line number Diff line number Diff line change 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 ( / ^ c o n t e n t \/ / , "" ) ;
10+ return `/${ locale } ${ clean } ` ;
11+ }
You can’t perform that action at this time.
0 commit comments