Streaming Rendering
Master streaming rendering — React Suspense-based streaming SSR, chunked transfer encoding, out-of-order HTML delivery, shell-first rendering, progressive page assembly, and error handling for streamed content to achieve the fastest possible Time to First Byte and progressive content delivery.
When to Use
- SSR blocks on slow data fetches, delaying TTFB for the entire page
- Users wait for the full page to render before seeing any content
- A page depends on multiple data sources with different response times
- Traditional SSR returns a blank screen until all data is fetched and rendered
- TTFB is high because the server waits for the slowest API before responding
- Above-fold content is ready quickly but below-fold content blocks the response
- A page mixes fast data (cached) with slow data (database, third-party API)
- Progressive enhancement requires content to appear incrementally
- React 18+ is available and streaming SSR can replace renderToString
- Server response is buffered entirely before sending, wasting time-to-first-byte potential
Instructions
Understand streaming versus buffered SSR. Traditional SSR buffers the entire HTML response. Streaming sends HTML chunks as they become ready:
Buffered SSR (renderToString):
Server: [fetch all data...400ms] [render...100ms] [send complete HTML]
Browser: [receive...parse...FCP]
TTFB: 500ms | FCP: 600ms
Streaming SSR (renderToPipeableStream):
Server: [render shell...20ms] [stream shell HTML] [fetch slow data...] [stream rest]
Browser: [receive shell...FCP] [...progressive content]
TTFB: 20ms | FCP: 100ms | Full content: 500ms
Structure the page with Suspense boundaries for streaming. Each Suspense boundary is a potential streaming point:
// The server streams each Suspense boundary independently
export default function DashboardPage() {
return (
<Shell>
{/* Streams immediately — no data dependency */}
<Header />
<Navigation />
<div className="dashboard-grid">
{/* Streams when metrics data resolves (~50ms) */}
<Suspense fallback={<MetricsSkeleton />}>
<MetricsPanel />
</Suspense>
{/* Streams when chart data resolves (~200ms) */}
<Suspense fallback={<ChartSkeleton />}>
<RevenueChart />
</Suspense>
{/* Streams when activity data resolves (~400ms) */}
<Suspense fallback={<ActivitySkeleton />}>
<RecentActivity />
</Suspense>
</div>
</Shell>
);
}
Implement streaming in a Node.js server. Use renderToPipeableStream with onShellReady:
import { renderToPipeableStream } from 'react-dom/server';
async function handleRequest(req: Request, res: Response) {
const { pipe, abort } = renderToPipeableStream(
<App url={req.url} />,
{
bootstrapScripts: ['/client.js'],
onShellReady() {
// The app shell (everything outside Suspense) is ready
// Start streaming immediately — don't wait for Suspense content
res.statusCode = 200;
res.setHeader('Content-Type', 'text/html; charset=utf-8');
res.setHeader('Transfer-Encoding', 'chunked');
pipe(res);
},
onAllReady() {
// Everything including Suspense content is ready
// For crawlers/bots, wait for this instead of onShellReady
},
onShellError(error) {
// Shell failed to render — send error page
res.statusCode = 500;
res.end('<h1>Something went wrong</h1>');
},
onError(error) {
// Non-shell error — log but continue streaming
console.error('Streaming error:', error);
},
}
);
// Abort streaming after timeout
setTimeout(() => abort(), 10000);
}
Handle bot/crawler requests differently. Detect bots via user-agent (/bot|crawl|spider/i). For users, call pipe(res) in onShellReady for fast TTFB. For bots, call pipe(res) in onAllReady so they receive complete HTML for SEO indexing.
Understand out-of-order streaming mechanics. React streams HTML in order (shell first), then replaces Suspense fallbacks out-of-order as data resolves:
<!-- Initial stream: shell + fallbacks -->
<html>
<body>
<header>...</header>
<div id="metrics">
<!--$?--><template id="B:0"></template>
<div class="skeleton">...</div>
<!--/$-->
</div>
<div id="chart">
<!--$?--><template id="B:1"></template>
<div class="skeleton">...</div>
<!--/$-->
</div>
<!-- Streamed later when metrics resolve (out of order, before chart): -->
<div hidden id="S:0"><div class="metrics-panel">Real metrics content...</div></div>
<script>
$RC('B:0', 'S:0');
</script>
<!-- $RC swaps the fallback template with the real content -->
<!-- Streamed even later when chart resolves: -->
<div hidden id="S:1"><div class="chart">Real chart content...</div></div>
<script>
$RC('B:1', 'S:1');
</script>
</body>
</html>
Implement error boundaries for streamed content. Individual Suspense sections can fail without crashing the entire stream:
// Error boundary catches failures in individual sections
function StreamSection({ children, fallback }) {
return (
<ErrorBoundary fallback={<SectionError />}>
<Suspense fallback={fallback}>
{children}
</Suspense>
</ErrorBoundary>
);
}
function DashboardPage() {
return (
<Shell>
<StreamSection fallback={<MetricsSkeleton />}>
<MetricsPanel /> {/* If this fails, shows SectionError */}
</StreamSection>
<StreamSection fallback={<ChartSkeleton />}>
<RevenueChart /> {/* Independent — unaffected by metrics failure */}
</StreamSection>
</Shell>
);
}
Configure streaming in Next.js App Router. Next.js App Router streams by default with loading.tsx files:
// app/dashboard/loading.tsx — automatic Suspense boundary
export default function DashboardLoading() {
return <DashboardSkeleton />;
}
// app/dashboard/page.tsx — async component triggers streaming
export default async function Dashboard() {
const metrics = await getMetrics(); // data fetch during streaming
return <MetricsDisplay data={metrics} />;
}
// Nested streaming with parallel data fetching:
// app/dashboard/@metrics/page.tsx — streams independently
// app/dashboard/@chart/page.tsx — streams independently
// app/dashboard/layout.tsx — renders the slot composition
Details
Chunked Transfer Encoding
Streaming SSR uses HTTP chunked transfer encoding (or HTTP/2 DATA frames). The server sends the response in chunks without knowing the total content length upfront. The browser incrementally parses and renders each chunk. This is a standard HTTP/1.1 feature (Transfer-Encoding: chunked) and is native to HTTP/2 and HTTP/3. No special client-side code is needed — browsers have always supported incremental HTML parsing.
Shell Content Selection
The "shell" is everything outside Suspense boundaries. Choosing what goes in the shell is a critical design decision. Shell content should be: (1) immediately available (no data fetching), (2) visually meaningful (layout, navigation, headers), (3) sufficient for LCP (if LCP is text-based). Shell content should NOT include: data-dependent content, personalized content, or content that requires slow API calls.
Worked Example: Vercel Dashboard
Five Suspense boundaries: navigation (shell, instant), project list (30ms from cache), deployment status (100ms from API), analytics (300ms), team activity (500ms). Users see navigation and project list within 50ms of TTFB. Perceived load is under 100ms despite the full page taking 600ms.
Anti-Patterns
Wrapping everything in a single Suspense boundary. One large Suspense boundary behaves like buffered SSR — nothing streams until all data resolves. Use multiple Suspense boundaries around independent data sources.
Putting the LCP element inside a Suspense boundary. If the LCP element (hero image, main heading) is inside Suspense, it will not stream with the shell. LCP content must be in the shell for optimal Core Web Vitals.
Not providing meaningful fallbacks. Suspense fallbacks that are empty or just a spinner waste the opportunity to show a structural preview. Use skeleton screens that match the loaded content's layout and dimensions to prevent CLS.
Ignoring streaming errors. The onError callback in renderToPipeableStream fires for non-shell errors. Without logging and error handling, failed Suspense boundaries silently show fallbacks permanently. Monitor streaming errors to detect data source failures.
Source
Process
- Read the instructions and examples in this document.
- Apply the patterns to your implementation, adapting to your specific context.
- Verify your implementation against the details and edge cases listed above.
Harness Integration
- Type: knowledge — this skill is a reference document, not a procedural workflow.
- No tools or state — consumed as context by other skills and agents.
Success Criteria
- TTFB is under 100ms for the shell content (navigation, layout, LCP elements).
- Each independent data source has its own Suspense boundary for parallel streaming.
- LCP content is in the shell, not inside a Suspense boundary.
- Suspense fallbacks use skeleton screens that match loaded content dimensions.
- Streaming errors are logged and individual section failures do not crash the page.
1---2name: perf-streaming-rendering3description: Streaming Rendering4metadata:5 source: https://skillmd.com/skills/intense-visions/perf-streaming-rendering6---7# Streaming Rendering89> Master streaming rendering — React Suspense-based streaming SSR, chunked transfer encoding, out-of-order HTML delivery, shell-first rendering, progressive page assembly, and error handling for streamed content to achieve the fastest possible Time to First Byte and progressive content delivery.1011## When to Use1213- SSR blocks on slow data fetches, delaying TTFB for the entire page14- Users wait for the full page to render before seeing any content15- A page depends on multiple data sources with different response times16- Traditional SSR returns a blank screen until all data is fetched and rendered17- TTFB is high because the server waits for the slowest API before responding18- Above-fold content is ready quickly but below-fold content blocks the response19- A page mixes fast data (cached) with slow data (database, third-party API)20- Progressive enhancement requires content to appear incrementally21- React 18+ is available and streaming SSR can replace renderToString22- Server response is buffered entirely before sending, wasting time-to-first-byte potential2324## Instructions25261. **Understand streaming versus buffered SSR.** Traditional SSR buffers the entire HTML response. Streaming sends HTML chunks as they become ready:2728 ```29 Buffered SSR (renderToString):30 Server: [fetch all data...400ms] [render...100ms] [send complete HTML]31 Browser: [receive...parse...FCP]32 TTFB: 500ms | FCP: 600ms3334 Streaming SSR (renderToPipeableStream):35 Server: [render shell...20ms] [stream shell HTML] [fetch slow data...] [stream rest]36 Browser: [receive shell...FCP] [...progressive content]37 TTFB: 20ms | FCP: 100ms | Full content: 500ms38 ```39402. **Structure the page with Suspense boundaries for streaming.** Each Suspense boundary is a potential streaming point:4142 ```typescript43 // The server streams each Suspense boundary independently44 export default function DashboardPage() {45 return (46 <Shell>47 {/* Streams immediately — no data dependency */}48 <Header />49 <Navigation />5051 <div className="dashboard-grid">52 {/* Streams when metrics data resolves (~50ms) */}53 <Suspense fallback={<MetricsSkeleton />}>54 <MetricsPanel />55 </Suspense>5657 {/* Streams when chart data resolves (~200ms) */}58 <Suspense fallback={<ChartSkeleton />}>59 <RevenueChart />60 </Suspense>6162 {/* Streams when activity data resolves (~400ms) */}63 <Suspense fallback={<ActivitySkeleton />}>64 <RecentActivity />65 </Suspense>66 </div>67 </Shell>68 );69 }70 ```71723. **Implement streaming in a Node.js server.** Use renderToPipeableStream with onShellReady:7374 ```typescript75 import { renderToPipeableStream } from 'react-dom/server';7677 async function handleRequest(req: Request, res: Response) {78 const { pipe, abort } = renderToPipeableStream(79 <App url={req.url} />,80 {81 bootstrapScripts: ['/client.js'],8283 onShellReady() {84 // The app shell (everything outside Suspense) is ready85 // Start streaming immediately — don't wait for Suspense content86 res.statusCode = 200;87 res.setHeader('Content-Type', 'text/html; charset=utf-8');88 res.setHeader('Transfer-Encoding', 'chunked');89 pipe(res);90 },9192 onAllReady() {93 // Everything including Suspense content is ready94 // For crawlers/bots, wait for this instead of onShellReady95 },9697 onShellError(error) {98 // Shell failed to render — send error page99 res.statusCode = 500;100 res.end('<h1>Something went wrong</h1>');101 },102103 onError(error) {104 // Non-shell error — log but continue streaming105 console.error('Streaming error:', error);106 },107 }108 );109110 // Abort streaming after timeout111 setTimeout(() => abort(), 10000);112 }113 ```1141154. **Handle bot/crawler requests differently.** Detect bots via user-agent (`/bot|crawl|spider/i`). For users, call `pipe(res)` in `onShellReady` for fast TTFB. For bots, call `pipe(res)` in `onAllReady` so they receive complete HTML for SEO indexing.1161175. **Understand out-of-order streaming mechanics.** React streams HTML in order (shell first), then replaces Suspense fallbacks out-of-order as data resolves:118119 ```html120 <!-- Initial stream: shell + fallbacks -->121 <html>122 <body>123 <header>...</header>124 <div id="metrics">125 <!--$?--><template id="B:0"></template>126 <div class="skeleton">...</div>127 <!--/$-->128 </div>129 <div id="chart">130 <!--$?--><template id="B:1"></template>131 <div class="skeleton">...</div>132 <!--/$-->133 </div>134135 <!-- Streamed later when metrics resolve (out of order, before chart): -->136 <div hidden id="S:0"><div class="metrics-panel">Real metrics content...</div></div>137 <script>138 $RC('B:0', 'S:0');139 </script>140 <!-- $RC swaps the fallback template with the real content -->141142 <!-- Streamed even later when chart resolves: -->143 <div hidden id="S:1"><div class="chart">Real chart content...</div></div>144 <script>145 $RC('B:1', 'S:1');146 </script>147 </body>148 </html>149 ```1501516. **Implement error boundaries for streamed content.** Individual Suspense sections can fail without crashing the entire stream:152153 ```typescript154 // Error boundary catches failures in individual sections155 function StreamSection({ children, fallback }) {156 return (157 <ErrorBoundary fallback={<SectionError />}>158 <Suspense fallback={fallback}>159 {children}160 </Suspense>161 </ErrorBoundary>162 );163 }164165 function DashboardPage() {166 return (167 <Shell>168 <StreamSection fallback={<MetricsSkeleton />}>169 <MetricsPanel /> {/* If this fails, shows SectionError */}170 </StreamSection>171172 <StreamSection fallback={<ChartSkeleton />}>173 <RevenueChart /> {/* Independent — unaffected by metrics failure */}174 </StreamSection>175 </Shell>176 );177 }178 ```1791807. **Configure streaming in Next.js App Router.** Next.js App Router streams by default with loading.tsx files:181182 ```typescript183 // app/dashboard/loading.tsx — automatic Suspense boundary184 export default function DashboardLoading() {185 return <DashboardSkeleton />;186 }187188 // app/dashboard/page.tsx — async component triggers streaming189 export default async function Dashboard() {190 const metrics = await getMetrics(); // data fetch during streaming191 return <MetricsDisplay data={metrics} />;192 }193194 // Nested streaming with parallel data fetching:195 // app/dashboard/@metrics/page.tsx — streams independently196 // app/dashboard/@chart/page.tsx — streams independently197 // app/dashboard/layout.tsx — renders the slot composition198 ```199200## Details201202### Chunked Transfer Encoding203204Streaming SSR uses HTTP chunked transfer encoding (or HTTP/2 DATA frames). The server sends the response in chunks without knowing the total content length upfront. The browser incrementally parses and renders each chunk. This is a standard HTTP/1.1 feature (Transfer-Encoding: chunked) and is native to HTTP/2 and HTTP/3. No special client-side code is needed — browsers have always supported incremental HTML parsing.205206### Shell Content Selection207208The "shell" is everything outside Suspense boundaries. Choosing what goes in the shell is a critical design decision. Shell content should be: (1) immediately available (no data fetching), (2) visually meaningful (layout, navigation, headers), (3) sufficient for LCP (if LCP is text-based). Shell content should NOT include: data-dependent content, personalized content, or content that requires slow API calls.209210### Worked Example: Vercel Dashboard211212Five Suspense boundaries: navigation (shell, instant), project list (~30ms from cache), deployment status (~100ms from API), analytics (~300ms), team activity (~500ms). Users see navigation and project list within 50ms of TTFB. Perceived load is under 100ms despite the full page taking 600ms.213214### Anti-Patterns215216**Wrapping everything in a single Suspense boundary.** One large Suspense boundary behaves like buffered SSR — nothing streams until all data resolves. Use multiple Suspense boundaries around independent data sources.217218**Putting the LCP element inside a Suspense boundary.** If the LCP element (hero image, main heading) is inside Suspense, it will not stream with the shell. LCP content must be in the shell for optimal Core Web Vitals.219220**Not providing meaningful fallbacks.** Suspense fallbacks that are empty or just a spinner waste the opportunity to show a structural preview. Use skeleton screens that match the loaded content's layout and dimensions to prevent CLS.221222**Ignoring streaming errors.** The onError callback in renderToPipeableStream fires for non-shell errors. Without logging and error handling, failed Suspense boundaries silently show fallbacks permanently. Monitor streaming errors to detect data source failures.223224## Source225226- React: Streaming SSR — https://react.dev/reference/react-dom/server/renderToPipeableStream227- Next.js: Streaming — https://nextjs.org/docs/app/building-your-application/routing/loading-ui-and-streaming228- Dan Abramov: "New Suspense SSR Architecture" — https://github.com/reactwg/react-18/discussions/37229- HTTP Chunked Transfer Encoding — https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Transfer-Encoding230231## Process2322331. Read the instructions and examples in this document.2342. Apply the patterns to your implementation, adapting to your specific context.2353. Verify your implementation against the details and edge cases listed above.236237## Harness Integration238239- **Type:** knowledge — this skill is a reference document, not a procedural workflow.240- **No tools or state** — consumed as context by other skills and agents.241242## Success Criteria243244- TTFB is under 100ms for the shell content (navigation, layout, LCP elements).245- Each independent data source has its own Suspense boundary for parallel streaming.246- LCP content is in the shell, not inside a Suspense boundary.247- Suspense fallbacks use skeleton screens that match loaded content dimensions.248- Streaming errors are logged and individual section failures do not crash the page.