Course map
Stage 1: App Router Foundations
Stage 2: The Rendering Model
Stage 3: Data, Caching and Mutations
Stage 4: Full-Stack Next.js
Stage 5: Production and Specialization

Part 13 / Topic 6 of 6

Cache Components classifies a GET Route Handler exactly like a Server Component

The installed route-handlers.md guide gives three worked examples for how cacheComponents: true (this install's default) treats a GET handler — a static one with no dynamic or uncached data, a dynamic one that reads a non-deterministic value, and a runtime-data one that reads headers()/cookies(). Two of those three are reproduced for real in this harness, side by side:

// harness/app/api/ops/ping/route.ts — no dynamic or uncached data at all
export async function GET() {
  return Response.json({ service: "quickcart-ops", status: "ok" });
}
// harness/app/api/ops/now/route.ts — reads headers(), a runtime API
import { headers } from "next/headers";
export async function GET() {
  const requestHeaders = await headers();
  const userAgent = requestHeaders.get("user-agent") ?? "unknown";
  return Response.json({ userAgent, checkedAt: new Date().toISOString() });
}

next build's own route table draws the line itself, with no extra config on either file:

├ ƒ /api/ops/now
├ ○ /api/ops/ping

○ (static) for ping, ƒ (dynamic) for now — exactly the classification Part 8/9 already taught for a Server Component, now shown to apply identically to a Route Handler. Grepping the real build artifact confirms ping's body was actually computed at build time, not just labeled static:

$ cat .next/server/app/api/ops/ping.body
{"service":"quickcart-ops","status":"ok"}

And a live request to the started server shows the cache actually serving that prerendered body:

$ curl -s -D - -o /dev/null http://localhost:PORT/api/ops/ping | grep -i "x-nextjs\|HTTP"
HTTP/1.1 200 OK
x-nextjs-cache: HIT
$ curl -s -D - -o /dev/null http://localhost:PORT/api/ops/now | grep -i "x-nextjs\|HTTP"
HTTP/1.1 200 OK

now gets no x-nextjs-cache header at all — it runs fresh on every request, exactly because headers() is a runtime API. The installed guide's own "Good to know" note lists the full trigger set for this: "Prerendering stops if the GET handler accesses network requests, database queries, async file system operations, request object properties..., runtime APIs like cookies(), headers(), connection(), or non-deterministic operations." It also states directly that use cache cannot be written inside a Route Handler's own body — it has to be extracted into a helper function the handler calls, the same constraint Part 10 already taught for Server Components.

Done here? The practice set for Part 13 — including the solutions — is on the part page.

Search the guide