Suspense, Streaming Data & React use()
Data fetching does not need to block the entire route equally.
A route can send useful UI while slower subtrees resolve:
request
β
server starts work
β
fast shell/content ready
β stream
slow subtree behind Suspense
β
replace fallback when ready
Route-level loadingβ
loading.tsx gives a route segment an immediate loading boundary.
Use it when a meaningful route-level skeleton can appear while the segment resolves.
app/projects/[projectId]/loading.tsx
This is useful but coarse.
Component-level Suspenseβ
For a slow widget:
import { Suspense } from 'react'
export default function Dashboard() {
return (
<main>
<Summary />
<Suspense fallback={<ActivitySkeleton />}>
<RecentActivity />
</Suspense>
</main>
)
}
Summary does not need to wait for RecentActivity if they are independent.
Boundary placement is product designβ
Do not add Suspense around every component.
Ask:
- What content must appear together?
- What can load independently?
- What skeleton is meaningful?
- Will layout shift be disruptive?
- Does deferred content affect the userβs next action?
A boundary defines a user-visible loading unit.
Start the promise on the serverβ
A Server Component can start work:
const commentsPromise = getComments(postId)
return <Comments commentsPromise={commentsPromise} />
A Client Component can receive the Promise and resolve it with React use():
'use client'
import { use } from 'react'
export function Comments({
commentsPromise,
}: {
commentsPromise: Promise<Comment[]>
}) {
const comments = use(commentsPromise)
return comments.map(comment => (
<article key={comment.id}>{comment.body}</article>
))
}
Wrap the consuming subtree in Suspense.
Why pass a Promise?β
This can let the server start secure data work early while a client component owns interactive rendering.
Server Component
β start data
Promise
β serialize/reference through RSC boundary
Client Component
β use(promise)
Suspense fallback until resolved
It does not mean the browser should receive database credentials or execute the server query.
Prefer Server Components when interactivity is unnecessaryβ
If the content is display-only:
async function Comments() {
const comments = await getComments()
return ...
}
is simpler than passing a Promise into a Client Component.
Use use() at the boundary when the client component genuinely owns interactive behavior.
Streaming does not make slow work fastβ
Suppose:
summary: 100 ms
activity: 2,000 ms
Streaming may let the summary appear around 100 ms while activity continues.
The 2,000 ms dependency is still slow.
You should still investigate:
- database query
- external API
- caching
- batching
- payload size
- architecture
Streaming improves delivery and perceived responsiveness; it is not a latency cure.
Avoid fallback waterfallsβ
Bad UX:
whole page skeleton
β
page appears
β
widget skeleton
β
widget appears
Sometimes nested boundaries create a sequence of placeholder swaps that feels noisy.
Design a stable shell and group related content.
Fallbacks should preserve layoutβ
Prefer:
function TableSkeleton() {
return <div className="table-skeleton" aria-hidden="true" />
}
with dimensions similar to the final table.
Avoid a tiny spinner that becomes a 1,000-pixel table and shifts the whole page.
Errors and Suspense are differentβ
Suspense handles waiting.
Error boundaries handle failures.
Promise pending β Suspense fallback
Promise rejected β error boundary
Promise fulfilled β content
Do not use βloading foreverβ as an error state.
Stream optional dependenciesβ
Example dashboard:
critical
βββ account identity
βββ permissions
βββ primary project data
streamable
βββ activity feed
βββ recommendations
βββ analytics chart
A slow recommendations service should not necessarily block a user from opening their project.
Security still happens before rendering dataβ
Do not stream unauthorized content and then hide it later.
The server data function must scope and authorize before the Promise produces sensitive data.
Sensitive promise outputsβ
Anything ultimately delivered to a Client Component should be treated as browser-visible.
Pass a minimal DTO:
type ActivityItemDTO = {
id: string
label: string
createdAt: string
}
not a full internal database model.
Client navigation and streamingβ
On subsequent App Router navigation, the client can request new server component payload data while preserving layouts.
A route loading boundary can give immediate feedback while the new server work resolves.
The deep transport mechanics belong to Phase 10, but the Phase 5 rule is:
data dependencies should align with useful loading boundaries.
use() is not an effect replacement for arbitrary browser fetchesβ
Do not translate every client useEffect(fetch) into a hand-built Promise created during render.
Use the pattern when React/Next.js owns a stable Promise/resource flow.
For browser-owned live data, a client data library may be more appropriate.
Common mistakesβ
Putting the whole route behind one Suspense fallbackβ
You lose the benefit of independent delivery.
Excessive tiny boundariesβ
The UI becomes a patchwork of placeholders.
Passing giant records through Promise propsβ
Minimize data at the server boundary.
Assuming streaming fixes database latencyβ
Measure the underlying dependency.
Using client rendering when a Server Component is enoughβ
Keep display-only data server-side.
Debugging checklistβ
- Identify which dependency actually suspends.
- Check whether it is critical or optional.
- Confirm independent work starts early.
- Inspect whether the fallback has stable dimensions.
- Verify rejected promises reach an error boundary.
- Confirm sensitive data is scoped before streaming.
- Inspect serialized data size.
- Test slow network and slow server scenarios separately.
- Test initial load and client navigation.
- Measure time to useful content, not only final completion.
Interview questionsβ
What does Suspense do for data fetching?
It lets a subtree declare a loading boundary while async work is pending, allowing other ready UI to render/stream independently.
Does streaming reduce server computation time?
No. It can improve when useful content is delivered, but slow dependencies remain slow.
Why pass a server-started Promise to a Client Component?
To start secure server work early while allowing an interactive client subtree to resolve the result with React use() under Suspense.
When should you avoid that pattern?
When the UI can remain a Server Component or when the data is truly browser-owned/live and better handled by a client data layer.
Exerciseβ
Design a dashboard with:
account summary
project table
activity feed
billing status
recommendations
Choose:
- which dependencies are critical
- which start in parallel
- route-level vs component Suspense boundaries
- fallback design
- error behavior
- which values may cross into Client Components
- what latency remains to optimize after streaming