10 min read
ā¢Question 33 of 47hardStreaming and Suspense in Depth
Advanced streaming patterns with React Suspense.
Streaming Architecture
How Streaming Works
code.jsJavaScript
Traditional SSR:
Request ā Fetch All ā Render All ā Send HTML ā Hydrate
Streaming:
Request ā Send Shell ā Stream Components ā Progressive HydrateSuspense Boundaries
code.txtTSX
import { Suspense } from 'react';
export default function Page() {
return (
<div>
<Header /> {/* Sent immediately */}
<Suspense fallback={<PostsSkeleton />}>
<Posts /> {/* Streamed when ready */}
</Suspense>
<Suspense fallback={<CommentsSkeleton />}>
<Comments /> {/* Streamed independently */}
</Suspense>
</div>
);
}
async function Posts() {
const posts = await fetchPosts(); // 2s delay
return <PostList posts={posts} />;
}
async function Comments() {
const comments = await fetchComments(); // 3s delay
return <CommentList comments={comments} />;
}Nested Suspense
code.txtTSX
<Suspense fallback={<PageSkeleton />}>
<Page>
<Suspense fallback={<SidebarSkeleton />}>
<Sidebar />
</Suspense>
<Suspense fallback={<ContentSkeleton />}>
<Content>
<Suspense fallback={<CommentsSkeleton />}>
<Comments />
</Suspense>
</Content>
</Suspense>
</Page>
</Suspense>Loading Priority
code.txtTSX
// loading.tsx - Automatic Suspense boundary
export default function Loading() {
return <Skeleton />;
}
// Use loading.tsx for route-level loading
// Use Suspense for component-level loading