Under The Hood
frontend nextjs Next.js 14+ / 15

App Route Handlers: Web Standard Response Stream Piping

Last updated
Prerequisites:
Web Streams API (ReadableStream, TransformStream)
Fetch API Request/Response
  • nextjs
  • route-handlers
  • web-streams
  • streaming
  • sse
  • api

Read at your depth

The practical view

Next.js App Router uses `app/api/.../route.ts` files to create API endpoints. Instead of Node.js `(req, res)` callbacks, route handlers use standard Web `Request` and `Response` objects. You can return a `new Response(readableStream)` to stream text, AI tokens, or binary data chunk-by-chunk to the client with `Transfer-Encoding: chunked`.

The same idea in other frameworks

react equivalent
Standard Web Fetch API / Node `ReadableStream` response handling.

Legacy vs modern

Legacy Buffered JSON Response vs Web Stream Piping

Buffered responses force clients to wait for full completion; Web Streams send progressive text/tokens in real time.

before → after
Pages API Buffered Route
res.status(200).json({ fullText }); // ❌ OLD: Waits for entire computation
App Route Handler Web Stream
return new Response(stream, { headers: { 'Content-Type': 'text/event-stream' } }); // ✅ NEW

Interview gotchas

Context

Interviewers test your knowledge of route handler evaluation rules.

The mechanical answer

In Next.js, a `GET` Route Handler is evaluated statically at build time by default if it does not access the `Request` object. To make it dynamic, you can export `export const dynamic恒 = 'force-dynamic'`, access request headers/cookies, or use other HTTP methods like `POST`, `PUT`, `DELETE`.

Trap

Do not assume all Route Handlers are dynamic. A parameter-less `GET` handler returning static data will be prerendered at build time.

Under The Hood — a multi-depth technical interview hub.

Third-party licenses Press ⌘ K to search.