Skip to content

Commit 6fc5ec7

Browse files
committed
fix(rank): HotDocsTab 走同源代理并解包 ApiResponse
- next.config.mjs 新增 /analytics/:path* rewrite 转发至后端 BACKEND_URL - 前端改用同源 /analytics/top-docs 请求,NEXT_PUBLIC_BACKEND_URL 作为可选覆盖(默认空串走代理),避免浏览器跨域 - 解包后端 ApiResponse 取 data 字段再渲染,之前直接当 HotDoc[] 用导致列表不显示
1 parent daeb444 commit 6fc5ec7

2 files changed

Lines changed: 16 additions & 8 deletions

File tree

app/components/rank/HotDocsTab.tsx

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,10 @@ function reducer(_: State, action: Action): State {
2727
return { status: "error" };
2828
}
2929

30-
const BACKEND_URL =
31-
process.env.NEXT_PUBLIC_BACKEND_URL ?? "http://localhost:8080";
30+
// 默认走 Next.js rewrite 同源代理(见 next.config.mjs 的 /analytics/:path*),
31+
// 若需要跨域直连后端(比如本地 Next.js 未启动但要用 curl/别的客户端测接口),
32+
// 可设置 NEXT_PUBLIC_BACKEND_URL=http://localhost:8081 覆盖。
33+
const BACKEND_URL = process.env.NEXT_PUBLIC_BACKEND_URL ?? "";
3234

3335
export function HotDocsTab({ initialWindow }: { initialWindow: WindowParam }) {
3436
const [windowParam, setWindowParam] = useReducer(
@@ -40,15 +42,17 @@ export function HotDocsTab({ initialWindow }: { initialWindow: WindowParam }) {
4042
useEffect(() => {
4143
dispatch({ type: "fetch" });
4244
let cancelled = false;
43-
fetch(
44-
`${BACKEND_URL}/api/v1/analytics/top-docs?window=${windowParam}&limit=20`,
45-
)
45+
fetch(`${BACKEND_URL}/analytics/top-docs?window=${windowParam}&limit=20`)
4646
.then((r) => {
4747
if (!r.ok) throw new Error();
48-
return r.json() as Promise<HotDoc[]>;
48+
return r.json() as Promise<{
49+
success: boolean;
50+
data: HotDoc[];
51+
}>;
4952
})
50-
.then((docs) => {
51-
if (!cancelled) dispatch({ type: "ok", docs });
53+
.then((body) => {
54+
if (!body.success) throw new Error();
55+
if (!cancelled) dispatch({ type: "ok", docs: body.data ?? [] });
5256
})
5357
.catch(() => {
5458
if (!cancelled) dispatch({ type: "error" });

next.config.mjs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,10 @@ const config = {
3636
source: "/auth/:path*",
3737
destination: `${backendUrl}/auth/:path*`,
3838
},
39+
{
40+
source: "/analytics/:path*",
41+
destination: `${backendUrl}/analytics/:path*`,
42+
},
3943
];
4044
},
4145
images: {

0 commit comments

Comments
 (0)