并行、串行与流式数据获取
通过并行获取数据、了解何时需要串行获取,以及结合 Suspense 进行流式传输,优化 App Router 的数据加载。
并行、串行与流式数据获取 是 CoddyKit 上的免费 Next.js 15 Fullstack (App Router + Server Actions) 课时。 这是第 3 节课,共 3 节。 你可以在下方免费阅读本课时的完整内容 — 然后在浏览器中使用内置代码编辑器和全天候 AI 导师进行实践。 这是 Next.js 15 Fullstack (App Router + Server Actions) 学习路径的一部分,你的进度在网页和 CoddyKit 应用中同步。 Next.js 15 Fullstack (App Router + Server Actions) 课程共包含 3 节课。
本课时的部分内容尚未翻译,以英文显示。
Two Fetching Shapes
Server Components can fetch data sequentially (one waits for another) or in parallel (all at once). Choosing well dramatically affects load time.
Sequential Fetching
Sometimes one request depends on another result, forcing order. This creates a waterfall and is slower.
const user = await getUser(id);
const posts = await getPosts(user.teamId); // needs user firstParallel Fetching
Independent requests should start together. Initiate the promises, then await them with Promise.all.
const userP = getUser(id);
const postsP = getPosts(id);
const [user, posts] = await Promise.all([userP, postsP]);Avoiding Waterfalls
An accidental waterfall happens when you await each fetch on its own line even though they are independent. Kick them off before awaiting.
Streaming with Suspense
You do not have to wait for everything. Wrap slow parts in Suspense so fast content streams to the user immediately.
import { Suspense } from "react";
<Suspense fallback={<Spinner />}>
<SlowComponent />
</Suspense>loading.tsx is Suspense
The special loading.tsx file is automatically a Suspense boundary for the whole route segment, giving an instant loading state.
Granular Streaming
Place multiple Suspense boundaries so each section reveals as soon as its own data is ready, instead of blocking on the slowest one.
Fetching at the Leaf
In RSC you can fetch data exactly where it is needed (in the component that uses it). React dedupes identical requests, so colocated fetches are fine.
Request Memoization
Next.js memoizes fetch calls with the same URL/options within a single render pass, so calling it in multiple components does not hit the network twice.
Preloading Data
You can start a fetch early (a preload function called before render) to overlap network time with rendering of other parts.
export function preload(id) { void getUser(id); }Picking a Strategy
Default to parallel + colocated fetching; add Suspense boundaries around slow, independent sections; only go sequential when a real data dependency exists.
Quick Check
How do you avoid a data fetching waterfall for two independent requests?
Recap
You learned to fetch in parallel with Promise.all, avoid accidental waterfalls, and use Suspense (including loading.tsx) to stream UI progressively. Next.js memoizes fetches, so colocated leaf fetching is efficient.
常见问题解答
「并行、串行与流式数据获取」课时是免费的吗?
是的 — 「并行、串行与流式数据获取」的完整文本可在网页上免费阅读。要进行交互式练习(内置代码编辑器和全天候 AI 导师)并解锁 Next.js 15 Fullstack (App Router + Server Actions) 课程的其余内容,请升级到 CoddyKit PRO。 Next.js 15 Fullstack (App Router + Server Actions) 课程共包含 3 节课。
「并行、串行与流式数据获取」这节课中我会学到什么?
通过并行获取数据、了解何时需要串行获取,以及结合 Suspense 进行流式传输,优化 App Router 的数据加载。 你通过在浏览器中直接运行的动手代码来练习 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) 课程适合初学者到高级学习者,你可以从这里开始或从头开始,按照自己的节奏学习。 这是第 3 节课,共 3 节。
「并行、串行与流式数据获取」课时需要多长时间?
大多数 CoddyKit 课程大约需要 5–10 分钟。每节课都很精短且互动,所以你能稳步进步,并在网页和应用中从离开的地方继续。
我能在这节 Next.js 15 Fullstack (App Router + Server Actions) 课中编写并运行代码吗?
能。每节 Next.js 15 Fullstack (App Router + Server Actions) 课都包含内置代码编辑器,你可以在浏览器中直接编写并运行真实代码,并获得即时 AI 反馈 — 无需本地设置。