Study notes · 10% of the exam

Route Handlers and APIs

Route Handlers are `route.ts` files that export one function per HTTP method and speak the Web Request/Response APIs. Know their defaults (not cached, params is a Promise, automatic HEAD and OPTIONS), how bodies, cookies and redirects behave, and when to use a handler instead of a Server Action or a direct data call.

Key points

  1. 1

    A route.ts exports named GET, POST, PUT, PATCH, DELETE, HEAD or OPTIONS functions. Other methods get 405. A route and a page can't share a segment.

  2. 2

    HEAD is answered by your GET when not exported, and OPTIONS gets an automatic 204 with an Allow header but no CORS headers.

  3. 3

    Since v15, GET handlers are not cached by default and context.params is a Promise. Without Cache Components, opt in with dynamic = 'force-static' and revalidate. With Cache Components, put use cache in a helper, not in the handler body.

  4. 4

    Read a body once with json(), formData(), text() or arrayBuffer(); clone() first if you need it twice. Webhooks must verify the raw text() before parsing.

  5. 5

    redirect() gives 307 (and keeps cookies set via cookies()), notFound() gives an empty 404, and both throw, so call them outside try.

  6. 6

    Fetch data in Server Components directly, not through your own handlers. Use handlers for public clients, webhooks and non-UI files; Server Actions are queued and meant for UI mutations.

  7. 7

    On serverless hosts there is no shared memory or durable disk, long requests hit maxDuration, and WebSockets don't work. The Edge Runtime is deprecated in Next.js 16.

Common traps

  • Re-serialising parsed JSON before checking a webhook signature makes every valid event fail.

  • A redirect() inside try { … } catch is swallowed by the catch block.

  • force-static doesn't fail on headers(); it silently returns empty values and caches them.

Test yourself on Route Handlers and APIs

Ten questions, with the answer and explanation after each one.