#1 Data Analytics Program in India
₹2,499₹1,499Enroll Now
10 min read
•Question 33 of 47hard

Streaming 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 Hydrate

Suspense 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