- feedData.ts 聚合首页/板块/帖子详情数据,缺失内容 throw render(404) - Go 侧 spa_meta/seo 的 head 生成逻辑移植到 src/ssr/head.ts - _error 页复用站点壳渲染 404,+data 从 abortReason 恢复壳层数据 - linkedom 模拟服务端 DOM,DOMPurify 封装为 SSR 透传的 sanitizeHtml - 新增 /api/home-boot 聚合端点(handler/home_boot.go)供 SSR 冷启动灌缓存
36 lines
1.2 KiB
TypeScript
36 lines
1.2 KiB
TypeScript
/**
|
||
* 服务端 API 客户端:Vike +data 在 Node 侧调用 Go API 用(仅 SSR 打包引用)。
|
||
*
|
||
* - GO_API_URL 指向 Go 服务(dev 默认与 Vite proxy 一致;生产由环境变量给 :3001)
|
||
* - 转发浏览器 cookie 以透传登录态
|
||
* - 转发 host / X-Forwarded-* 让 Go 推导正确的站点 base URL(canonical/og:url 等)
|
||
* - 5s 超时;任何失败返回 null,调用方降级为 SPA 壳,不阻断渲染
|
||
*/
|
||
|
||
const GO_API_URL = process.env.GO_API_URL || `http://127.0.0.1:${process.env.VITE_API_PORT || '3000'}`;
|
||
|
||
export type ServerApiOptions = {
|
||
/** 浏览器请求的 cookie 原样转发 */
|
||
cookie?: string;
|
||
/** 额外转发给 Go 的请求头(host / x-forwarded-* 等) */
|
||
headers?: Record<string, string>;
|
||
timeoutMs?: number;
|
||
};
|
||
|
||
export async function serverApi<T>(path: string, opts?: ServerApiOptions): Promise<T | null> {
|
||
try {
|
||
const res = await fetch(`${GO_API_URL}${path}`, {
|
||
headers: {
|
||
...(opts?.cookie ? { cookie: opts.cookie } : {}),
|
||
...(opts?.headers ?? {}),
|
||
},
|
||
signal: AbortSignal.timeout(opts?.timeoutMs ?? 5000),
|
||
cache: 'no-store',
|
||
});
|
||
if (!res.ok) return null;
|
||
return (await res.json()) as T;
|
||
} catch {
|
||
return null;
|
||
}
|
||
}
|