perf(homepage): 首页 SSG 化,剩 25% Vercel CPU 归零#332
Merged
longsizhuo merged 3 commits intomainfrom May 6, 2026
Merged
Conversation
i18n PR 收尾。app/[locale]/page.tsx 之前 await fetchHomepageEvents()
让首页钉成 ƒ Dynamic(任何 server fetch 都阻挡 SSG),首页占 30 天
Vercel Fluid CPU ~25%。
改造:
- 新建 /api/public/homepage-events 和 /api/public/top-docs ISR 代理
(revalidate=300,浏览器 Cache-Control 5min),让 server 端的 backend
fetch 集中在两条静态化的 API route 里,最多每 5min 命中一次
- ActivityTicker 从 async server component 改 client,useEffect 自取
- HotDocsPreview 同样改 client(翻译用 useTranslations 替代
getTranslations),Skeleton 在 fetch 期间显示
- FloatWindow 从 props 接收 event 改为自取,page.tsx 不再传
- app/[locale]/page.tsx 加 force-static + setRequestLocale,纯静态
build 表(验证):
/ → ● /[locale]
├ /zh
└ /en
/api/public/homepage-events → ○ Static (5m revalidate)
/api/public/top-docs → ○ Static (5m revalidate)
预期 Vercel Fluid CPU 从月 ~3h51m 降到月 0.5-1h(仅 admin/events/feed
等动态路由产生)。
|
The latest updates on your projects. Learn more about Vercel for GitHub.
|
3 tasks
补两条 i18n 收尾的零碎: 1. app/[locale]/page.tsx 加 generateMetadata 覆盖 root layout 默认 alternates,给首页输出当前 locale 的 canonical (/zh 或 /en),并且 hreflang 三向声明(zh-CN / en-US / x-default)。 每个 locale 各自 canonical,不再共享 root 的 fallback。 2. dev_docs/i18n_url_routing.md 加「如何加新 user-facing 路由」章节 贴出 page.tsx + generateMetadata 的 boilerplate,列出 5 条容易踩的坑: - setRequestLocale 排序 - @/i18n/navigation vs next/navigation 选错的后果 - components 不在 [locale] 下 - server fetch 让 page 退回 dynamic 的应对 - layout 嵌套也要逐层 setRequestLocale 后续加 page 直接抄就能保证 SSG + i18n 一起对。
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
背景
i18n PR (#330) 让 docs 全量 SSG,剩首页(占 30 天 Vercel Fluid CPU ~25%)仍 ƒ Dynamic。原因:
Hero内的HotDocsPreview和ActivityTicker也是 async server component,server fetch backend 同样阻挡 SSG。改造
1. 新建两条 ISR 代理 API
/api/public/homepage-events/api/public/top-docsserver-side 调后端,浏览器调自家代理(同源无 CORS,BACKEND_URL 不暴露),revalidate 让 Next.js Data Cache + 浏览器 Cache-Control 双保险,最多每 5min 一次后端调用。
2. 三个组件改 client
ActivityTickerasync server → client + useEffect fetchHotDocsPreviewasync server → client;翻译从getTranslations改useTranslations(client hook);fetch 期间显示 SkeletonFloatWindow从 props 接 event 改为自取3. page.tsx 纯静态
await fetchHomepageEvents()+ 删 prop 传递export const dynamic = 'force-static'+setRequestLocale双保险build 验证
322 → 361 prerendered routes(多了 /zh、/en、+ 两条 ISR API)。
预期收益
副作用
Test plan
/zh看 FloatWindow 1-2s 内显示/[locale]的 invocation 数应该接近 0/api/public/homepage-events返回正常 JSON