0Pricing
Sveltejs Academy · Lesson

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

  1. Streaming Data with Promises in load
  2. Deferred Loading and Suspense-Like UX
  3. Streaming from Server with +server.js
  4. Progressive Hydration
← Back to Sveltejs Academy