Next.js Developer interview question · Most asked
What are Route Handlers and when should you use them instead of Server Actions?
Summary
Route Handlers (app/api/.../route.ts) export functions named after HTTP methods (GET, POST, ...) using Web Request/Response APIs. Use them for public REST endpoints, webhooks, third-party callbacks, file downloads and mobile clients; use Server Actions for mutations triggered from your own UI.
Longer Answer
Route Handlers support streaming responses, NextResponse.json(), cookies and headers, and can run on Node or Edge runtimes. In Next.js 15+, GET handlers aren't cached by default (export const dynamic = 'force-static' opts in).
Code Example
// app/api/webhooks/stripe/route.ts
import { NextResponse } from "next/server";
export async function POST(req: Request) {
const body = await req.text();
const sig = req.headers.get("stripe-signature")!;
const event = stripe.webhooks.constructEvent(body, sig, process.env.STRIPE_WEBHOOK_SECRET!);
await handleEvent(event);
return NextResponse.json({ received: true });
}
Key Points
- Export
GET,POST, etc. fromroute.ts - Web Request/Response APIs
- Webhooks, public APIs, mobile clients
- Server Actions for UI mutations
More most-asked Next.js Developer questions
- How did caching defaults change in Next.js 15?
- What happens, step by step, when a request reaches a Next.js App Router application?
- How does data fetching work in the App Router?
- What are the differences between the App Router and the Pages Router?
- What is the difference between Server Components and Client Components?
- Explain the App Router file conventions: page, layout, loading, error, not-found, template and route.
- What is Next.js and what problems does it solve on top of React?
- Explain SSR, SSG, ISR and CSR in Next.js.