🌐 Web Development

Streaming SSR

Streaming SSR

Sends server-rendered results as a stream. Improves TTFB.

πŸ“– Detailed Explanation

Streaming SSR is a technique that progressively transmits server-rendered HTML in chunks. Rather than waiting until the entire page is complete, it sends the parts that are ready to the client first, greatly improving TTFB (Time To First Byte).

Combining React 18's renderToPipeableStream with Suspense enables you to implement Streaming SSR. Components that render quickly are sent first, while components that need data fetching show a fallback and are replaced once they are ready.

In the Next.js App Router, Streaming is applied automatically by using a loading.js file or a Suspense boundary. Users see the page shell immediately and experience the corresponding area being filled in once the data has loaded.

Streaming SSR is particularly effective on pages where database queries or external API calls take a long time. It shows the core content first and loads supplementary information (comments, recommendations, etc.) later, improving perceived performance.

πŸ’» Code Example

// Next.js App Router - Streaming with Suspense
// app/post/[id]/page.tsx

import { Suspense } from 'react';

// μ¦‰μ‹œ λ Œλ”λ§λ˜λŠ” λΆ€λΆ„
async function PostContent({ id }: { id: string }) {
  const post = await fetch(`https://api.example.com/posts/${id}`, {
    cache: 'no-store'
  }).then(r => r.json());

  return (
    <article>
      <h1>{post.title}</h1>
      <div>{post.content}</div>
    </article>
  );
}

// 느린 데이터 λ‘œλ”©
async function Comments({ postId }: { postId: string }) {
  // μ˜λ„μ μœΌλ‘œ 느린 API
  const comments = await fetch(
    `https://api.example.com/posts/${postId}/comments`,
    { cache: 'no-store' }
  ).then(r => r.json());

  return (
    <section>
      <h2>λŒ“κΈ€ ({comments.length})</h2>
      {comments.map(c => <Comment key={c.id} {...c} />)}
    </section>
  );
}

// μΆ”μ²œ κ²Œμ‹œλ¬Ό (κ°€μž₯ 느림)
async function RelatedPosts({ postId }: { postId: string }) {
  const related = await getRelatedPosts(postId);
  return <RelatedPostList posts={related} />;
}

// 메인 νŽ˜μ΄μ§€ - Streaming 적용
export default async function PostPage({
  params
}: { params: { id: string } }) {
  return (
    <main>
      {/* λ¨Όμ € 전솑됨 */}
      <Suspense fallback={<PostSkeleton />}>
        <PostContent id={params.id} />
      </Suspense>

      {/* 두 번째둜 전솑됨 */}
      <Suspense fallback={<CommentsSkeleton />}>
        <Comments postId={params.id} />
      </Suspense>

      {/* λ§ˆμ§€λ§‰μ— 전솑됨 */}
      <Suspense fallback={<RelatedSkeleton />}>
        <RelatedPosts postId={params.id} />
      </Suspense>
    </main>
  );
}

// loading.js - 라우트 레벨 λ‘œλ”© UI
// app/post/[id]/loading.tsx
export default function Loading() {
  return (
    <div className="animate-pulse">
      <div className="h-8 bg-gray-200 rounded w-3/4 mb-4" />
      <div className="h-4 bg-gray-200 rounded w-full mb-2" />
      <div className="h-4 bg-gray-200 rounded w-5/6" />
    </div>
  );
}

πŸ—£οΈ Examples of Practical Conversations

In a technical meeting:

"The product detail page TTFB takes as long as 2 seconds. If we apply Streaming SSR, we can show the product information in 200ms and load the reviews and recommended products later with Suspense, which makes the perceived speed much faster."

In a technical interview:

"How does Streaming SSR work?" - "It splits the HTML into pieces and sends them using Transfer-Encoding: chunked. It sends a placeholder for each Suspense boundary, and when the data is ready, it injects the actual content with a script tag."

In a code review:

"If you split Suspense boundaries too small, performance actually gets worse. Wrap components that are logically grouped together in a single Suspense, and separate only those that can be loaded independently."

Take a mock interview on this topic β†’ WIA SOOM AI Interview Β· Start for free

⚠️ Cautions

πŸ”— Related Terms

SSR Suspense React Server Components Hydration Next.js

πŸ“š Learn More

πŸ“„ Next.js Official Documentation - Loading UI and Streaming πŸ“„ React - renderToPipeableStream πŸ“„ web.dev - Streaming SSR with React