05 / 10

How do you implement a streaming response from a Route Handler in Next.js?

In Next.js App Router, Route Handlers can stream responses using the Web Streams API via ReadableStream or TransformStream. This allows data to be sent to the client progressively — chunk by chunk — instead of waiting for the entire response to be ready.

Streaming is useful when you have long-running operations like AI text generation, large data exports, or real-time logs. Instead of the client waiting for the full response, it starts receiving and rendering data immediately as chunks arrive.

When to Use Streaming in Route Handlers
  1. 1

    AI/LLM responses — stream tokens as they are generated (e.g. ChatGPT-style)

  2. 2

    Large data exports — stream CSV/JSON rows without loading all data into memory

  3. 3

    Real-time logs — stream server logs or job progress to the client

  4. 4

    Long-running computations — send partial results as they become available

  5. 5

    Server-Sent Events (SSE) — push updates from server to client over HTTP

Basic Streaming Route Handler using ReadableStream
Streaming AI/LLM Response (OpenAI style)
Server-Sent Events (SSE) Stream
Consuming the Stream on the Client Side
Consuming SSE Stream on the Client
Key Rules and Gotchas
  1. 1

    Always set 'Cache-Control: no-cache' for streaming responses to prevent buffering

  2. 2

    Use 'Transfer-Encoding: chunked' for plain streams and 'text/event-stream' for SSE

  3. 3

    Always call controller.close() when done — leaving streams open causes memory leaks

  4. 4

    Edge Runtime supports streaming natively and has lower latency than Node.js runtime

  5. 5

    Node.js runtime may buffer small chunks — use flush() or increase chunk size if needed

  6. 6

    ReadableStream is a Web API — available in both Edge and Node.js runtimes in Next.js 13+

  7. 7

    For AI streaming, Vercel AI SDK abstracts all of this with useChat and useCompletion hooks

ReadableStream vs SSE vs WebSocket — When to Use Each
  1. 1

    ReadableStream — one-time large response, file downloads, AI completions

  2. 2

    SSE (text/event-stream) — server pushing multiple updates over time, live feeds, notifications

  3. 3

    WebSocket — full duplex, both client and server send messages, chat apps, collaboration tools

Difficulty: 6/10
Topics: API Routes, Streaming, Response handling

Scenario Questions

0-2 years experience
  1. 1

    We need to send a large CSV file to the client using a Next.js route handler. How would you set up the handler to stream the file instead of loading it all into memory?

  2. 2

    If you forget to set the correct headers when streaming a response, what behavior would the browser exhibit?

2-5 years experience
  1. 1

    You added a streaming response to an API route, but the client sometimes receives a truncated response under load. Walk me through how you'd debug this.

  2. 2

    Explain the trade‑offs between using Next.js Edge Functions versus Node.js route handlers for streaming large JSON payloads.

5-8 years experience
  1. 1

    Our service streams video chunks from an external service through a Next.js route. How would you design the handler to handle back‑pressure and ensure low latency at scale?

  2. 2

    What considerations would you make for caching and CDN when serving streamed responses from Next.js, and how would you configure them?

8+ years experience
  1. 1

    We plan to migrate several legacy monolith endpoints to Next.js with streaming responses. How would you structure the architecture to minimize impact on existing services and allow incremental rollout?

  2. 2

    Discuss the long‑term maintenance implications of using streaming responses in a micro‑frontend architecture that shares a Next.js app across teams.

Follow-up Questions

  • How would you test the streaming endpoint locally and in CI?
  • What metrics would you monitor in production for a streaming API?
  • Are there any security considerations when streaming data?