Question presented to candidate: "When would you write a Route Handler instead of a Server Action? Give me concrete cases for each."
What a strong answer should cover:
- Server Actions: functions you call from your own React UI; Next.js generates the endpoint, uses POST, handles serialization and revalidation in the same round trip.
- Route Handlers:
route.tsfiles exporting HTTP methods; you design the URL, method, status codes and payloads for any client. - Use actions for mutations triggered by your own forms and buttons.
- Use Route Handlers for webhooks, public or mobile APIs, file downloads, GET endpoints that others call, and anything that needs custom headers or streaming responses.
- Both run on the server and both need their own authentication.
Clarifying questions expected:
- "Who calls this: our React UI or someone else?" β the main deciding question.
- "Does the caller need a stable URL and method?" β only Route Handlers give that.
Code / implementation expected: Yes β the same mutation as an action and as a Route Handler.