Streaming from Server with +server.js
Emit chunks progressively from an API route using a ReadableStream.
Server-Sent Streams
Inside +server.js, return a Response wrapping a ReadableStream.
Basic Stream
Build a stream that emits chunks over time.
export function GET() {
const stream = new ReadableStream({
async start(controller) {
for (const chunk of source) {
controller.enqueue(new TextEncoder().encode(chunk));
}
controller.close();
}
});
return new Response(stream);
}All lessons in this course
- Streaming Data with Promises in load
- Deferred Loading and Suspense-Like UX
- Streaming from Server with +server.js
- Progressive Hydration