Streaming with ReadableStream
Stream large responses progressively using the Web Streams API in +server.js.
Why Stream
Large responses can be sent progressively so the client starts receiving data immediately.
ReadableStream API
Use the standard Web Streams API to build a streaming response.
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
- +server.js: GET POST PUT DELETE
- Request and RequestEvent
- Returning JSON Responses
- Streaming with ReadableStream