Performance: Streaming long operations
This affects how quickly users see content during long server operations by sending partial data progressively.
import React from 'react'; export default async function Page() { const stream = fetchLongDataStream(); return ( <div> <React.Suspense fallback={<Loading />}> <StreamingContent stream={stream} /> </React.Suspense> </div> ); }
export default async function Page() { const data = await fetchLongData(); return <div>{data}</div>; }
| Pattern | DOM Operations | Reflows | Paint Cost | Verdict |
|---|---|---|---|---|
| Blocking full data fetch | Single large DOM update after full data | 1 reflow after full data | High paint cost due to large update | [X] Bad |
| Streaming partial content | Multiple small DOM updates progressively | Multiple small reflows but faster perceived paint | Lower paint cost per chunk | [OK] Good |
export async function GET() {
const stream = new ReadableStream({
async start(controller) {
controller.enqueue(new TextEncoder().encode('Hello '));
await new Promise(r => setTimeout(r, 1000));
controller.enqueue(new TextEncoder().encode('World!'));
controller.close();
}
});
return new Response(stream);
}export async function GET() {
const stream = new ReadableStream({
start(controller) {
controller.enqueue('Data chunk');
// Missing controller.close()
}
});
return new Response(stream);
}Pass the test and this pattern moves unknown