使用 Suspense 与加载状态实现界面流式传输
使用 React Suspense、loading.tsx 文件和骨架屏流式传输服务器渲染的界面,提升感知性能,让用户更快看到内容。
使用 Suspense 与加载状态实现界面流式传输 是 CoddyKit 上的免费 Next.js 15 Fullstack (App Router + Server Actions) 课时。 这是第 4 节课,共 4 节。 你可以在下方免费阅读本课时的完整内容 — 然后在浏览器中使用内置代码编辑器和全天候 AI 导师进行实践。 这是 Next.js 15 Fullstack (App Router + Server Actions) 学习路径的一部分,你的进度在网页和 CoddyKit 应用中同步。 Next.js 15 Fullstack (App Router + Server Actions) 课程共包含 4 节课。
本课时的部分内容尚未翻译,以英文显示。
The Waterfall Problem
If a page waits for every data fetch before rendering, users stare at a blank screen. Streaming lets the server send ready parts of the page immediately and fill in slow parts as they finish.
What is Streaming SSR?
Next.js with the App Router can stream HTML in chunks. The shell and fast components arrive first; slow data-dependent sections stream in when ready, improving Time to First Byte and perceived speed.
React Suspense Basics
Suspense wraps a component that may not be ready and shows a fallback until it is. The rest of the page renders without waiting.
import { Suspense } from 'react';
<Suspense fallback={<p>Loading...</p>}>
<SlowComponent />
</Suspense>Async Server Components
In the App Router, a Server Component can be async and await data. Wrapping it in Suspense lets the page stream while that fetch resolves.
async function Reviews() {
const data = await getReviews();
return <ReviewList items={data} />;
}Streaming a Slow Section
Render the page shell instantly and stream the slow part. Users interact with the rest while reviews load.
export default function Page() {
return (
<main>
<Header />
<Suspense fallback={<ReviewsSkeleton />}>
<Reviews />
</Suspense>
</main>
);
}The loading.tsx Convention
A loading.tsx file beside a route automatically wraps that route's page in Suspense. Its export is shown instantly while the page's data loads.
// app/dashboard/loading.tsx
export default function Loading() {
return <DashboardSkeleton />;
}Building a Skeleton
A skeleton mimics the final layout with gray placeholders, reducing layout shift and signaling that content is coming.
function ReviewsSkeleton() {
return (
<div className="animate-pulse space-y-2">
<div className="h-4 bg-gray-200 rounded" />
<div className="h-4 bg-gray-200 rounded w-3/4" />
</div>
);
}Parallel Data Fetching
Avoid sequential awaits that create waterfalls. Multiple Suspense boundaries let independent sections fetch in parallel and stream as each completes.
<Suspense fallback={<A />}><Sales /></Suspense>
<Suspense fallback={<B />}><Traffic /></Suspense>Granular Boundaries
Place Suspense around the smallest slow unit, not the whole page. Finer boundaries mean more of the UI is interactive sooner.
Streaming vs Static
Streaming shines for personalized or slow data. For content that rarely changes, static generation or caching is still faster. Combine both: cache what you can, stream the rest.
Best Practices
Stream effectively:
- Wrap slow async Server Components in Suspense
- Use loading.tsx for route-level fallbacks
- Show skeletons to cut layout shift
- Use parallel boundaries to avoid waterfalls
Quick Check
Test your streaming knowledge.
Recap
You learned to stream UI:
- Streaming SSR sends ready HTML first and fills slow parts later
- Wrap async components in
Suspensewith a fallback - Use
loading.tsxfor automatic route fallbacks - Show skeletons and use parallel boundaries
Your pages now feel fast even with slow data.
用 AI 导师学习 TypeScript — 免费
在浏览器中编写并运行真实代码,获得全天候 AI 导师的即时帮助,并在网页或应用中继续学习。
- 课程
- 22
- 课程
- 88
常见问题解答
「使用 Suspense 与加载状态实现界面流式传输」课时是免费的吗?
是的 — 「使用 Suspense 与加载状态实现界面流式传输」的完整文本可在网页上免费阅读。要进行交互式练习(内置代码编辑器和全天候 AI 导师)并解锁 Next.js 15 Fullstack (App Router + Server Actions) 课程的其余内容,请升级到 CoddyKit PRO。 Next.js 15 Fullstack (App Router + Server Actions) 课程共包含 4 节课。
「使用 Suspense 与加载状态实现界面流式传输」这节课中我会学到什么?
使用 React Suspense、loading.tsx 文件和骨架屏流式传输服务器渲染的界面,提升感知性能,让用户更快看到内容。 你通过在浏览器中直接运行的动手代码来练习 Next.js 15 Fullstack (App Router + Server Actions),全天候 AI 导师会在你学习这节课的过程中回答你的问题。
学习 Next.js 15 Fullstack (App Router + Server Actions) 需要有经验吗?
无需任何先前经验。CoddyKit 上的 Next.js 15 Fullstack (App Router + Server Actions) 课程适合初学者到高级学习者,你可以从这里开始或从头开始,按照自己的节奏学习。 这是第 4 节课,共 4 节。
「使用 Suspense 与加载状态实现界面流式传输」课时需要多长时间?
大多数 CoddyKit 课程大约需要 5–10 分钟。每节课都很精短且互动,所以你能稳步进步,并在网页和应用中从离开的地方继续。
我能在这节 Next.js 15 Fullstack (App Router + Server Actions) 课中编写并运行代码吗?
能。每节 Next.js 15 Fullstack (App Router + Server Actions) 课都包含内置代码编辑器,你可以在浏览器中直接编写并运行真实代码,并获得即时 AI 反馈 — 无需本地设置。