在 Next.js App Router 中用服务端组件设计博客
以服务端组件为中心划分数据流,博客的缓存策略会变得简单。本文整理实际结构与常见陷阱。
好评 100%
核心摘要 · TL;DR
博客的读取远多于写入。列表与文章页在服务端渲染并用 ISR 更新,只有投票、阅读数等会变化的部分才拆分为客户端组件。
Key facts
- An ISR window of about 30 seconds is enough for most article pages.
- Side effects such as view counting belong in an API route, not in render.
- Admin areas should enforce both middleware and a server-side session check.
先理清结构
App Router 中最重要的决定是:哪些逻辑留在服务端。
- 列表、文章、分类页面:服务端组件 + ISR
- 投票、阅读数、阅读进度:客户端组件
- 管理后台表单:服务端操作
缓存策略
在文章页加一行再验证时间,多数博客就能保持足够的新鲜度。
常见错误
- 在渲染过程中修改数据。
- 调用 cookies() 让静态页面变成动态渲染。
- 把 Markdown 解析器打进客户端包。
总结
以读取为主的服务,服务端渲染加最小化客户端状态最简单也最快。
常见问题
Should view counts be incremented inside a server component?
No. Rendering can run multiple times and prefetching happens too. Increment views from a client call or a route handler.
这篇文章对你有帮助吗?
你的评价会告诉其他读者这篇文章的可信度。
好评 100%
Daniel Okafor
Senior data editor · InsightLog
Builds data pipelines and measurement systems, and writes about the context behind every number.
Markdown 原文: /zh/posts/nextjs-app-router-blog-architecture/raw
引用来源: https://insightlog.site/zh/posts/nextjs-app-router-blog-architecture