Streaming SSR
Streaming SSR
Sends server-rendered results as a stream. Improves TTFB.
Streaming SSR
Sends server-rendered results as a stream. Improves TTFB.
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.
// 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>
);
}
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