Next.js has changed how server-side code is written. With the App Router, React Server Components fetch data on the server and Server Actions (part of React’s Server Functions) handle form submissions and mutations without a hand-written API. That leads many developers to ask whether Route Handlers are still needed.
Quick answer: no, Server Actions and Server Components do not replace Route Handlers. Use Server Components to read data for pages, Server Actions for mutations triggered by your own UI (forms, buttons), and Route Handlers when you need a real HTTP endpoint: webhooks, public or mobile APIs, file downloads, streaming, OAuth callbacks or custom caching and headers.
Server Components vs Server Actions vs Route Handlers
| Server Components | Server Actions | Route Handlers | |
|---|---|---|---|
| Purpose | Render UI and read data on the server | Mutate data from your own UI | Expose HTTP endpoints |
| HTTP methods | n/a (rendered on request or at build) | POST only, called by Next.js | GET, POST, PUT, PATCH, DELETE, HEAD, OPTIONS |
| Called by | Next.js rendering | Forms and client components in your app | Any client: browsers, mobile apps, third parties |
| Typical uses | Pages, dashboards, product lists | Create, update, delete, form submissions | Webhooks, public APIs, file downloads, streaming |
| File | page.tsx, layout.tsx, components | Functions with 'use server' | route.ts in the app directory |
Understanding Route Handlers in Next.js
Route Handlers are the App Router replacement for the Pages Router’s pages/api routes. You create a route.ts (or route.js) file anywhere in the app directory, commonly under app/api, and export functions named after HTTP methods. They use the standard Web Request and Response APIs.
// app/api/orders/route.ts
import { NextResponse } from 'next/server';
export async function GET(request: Request) {
const orders = await db.order.findMany();
return NextResponse.json(orders);
}
Key benefits of Route Handlers:
- Full control of HTTP methods, status codes, headers and caching
- Can be called by any client, including mobile apps and partner systems
- Ideal for webhooks (Stripe, Shopify, GitHub), OAuth callbacks and file or stream responses
- Run on the Node.js or Edge runtime
What are Server Actions in Next.js?
Server Actions are asynchronous functions marked with 'use server' that run on the server but can be called directly from forms and client components. Next.js creates the endpoint for you, so you do not need to write an API route for your own UI.
// app/actions.ts
'use server';
import { revalidatePath } from 'next/cache';
import { z } from 'zod';
const schema = z.object({ name: z.string().min(1), email: z.string().email() });
export async function createUser(formData: FormData) {
const session = await auth();
if (!session) throw new Error('Unauthorized');
const data = schema.parse({
name: formData.get('name'),
email: formData.get('email'),
});
await db.user.create({ data });
revalidatePath('/users');
}
// app/users/new/page.tsx
import { createUser } from '@/app/actions';
export default function NewUserPage() {
return (
<form action={createUser}>
<input name="name" />
<input name="email" type="email" />
<button type="submit">Create user</button>
</form>
);
}
Advantages of Server Actions:
- No separate API route for your own forms and buttons
- Forms work before JavaScript loads (progressive enhancement)
- Built-in integration with caching and revalidation (
revalidatePath,revalidateTag) - Works with React hooks such as
useActionStateanduseFormStatusfor pending and error states
Security note: every Server Action is a publicly reachable POST endpoint. Always check authentication and authorisation, and validate input, inside each action, just as you would in an API route.
Free resource
Upgrade and patching checklist
Want this applied to your project? Our engineers will send it with notes for your stack.
What are Server Components?
Server Components render on the server and send HTML and a compact payload to the browser, without shipping their JavaScript to the client. In the App Router, components are Server Components by default.
Key benefits of Server Components:
- Smaller client-side JavaScript bundles
- Fetch data directly from databases or APIs during rendering
- Keep secrets and sensitive logic on the server
- Combine with Client Components (
'use client') only where interactivity is needed
For reading data in your own pages, a Server Component that queries the database directly usually replaces the old pattern of calling your own API route from the browser.
Do Server Actions and Components make Route Handlers obsolete?
No. They remove the need for many internal API routes, but Route Handlers remain essential for anything that is not your own Next.js UI.
Use Server Components when
- Fetching data to render a page or layout
- Building dashboards, listings and detail pages
- Keeping database queries and API keys on the server
Use Server Actions when
- Handling form submissions and simple mutations from your own app
- Creating, updating or deleting records and refreshing the page data
- You want progressive enhancement and less client-side code
Use Route Handlers when
- Receiving webhooks from payment providers, eCommerce platforms or other services
- Providing a public or mobile API used by other clients
- Handling OAuth callbacks and authentication flows
- Returning files, images, RSS feeds, sitemaps or streams
- You need GET requests with custom caching, CORS or headers
Common mistakes to avoid
- Using a Server Action to fetch data for rendering; use a Server Component instead.
- Calling your own Route Handler from a Server Component; call the data function directly.
- Skipping authorisation checks in Server Actions because they “feel internal”.
- Exposing a Server Action to external clients; build a Route Handler for them.
The future of data handling in Next.js
Next.js is moving towards a model where most reads happen in Server Components and most UI mutations use Server Actions, while Route Handlers provide the API surface for the outside world. For most applications, a hybrid approach is the right one.
Frequently asked questions
Do I still need API routes in Next.js? For your own UI, often not: use Server Components for reads and Server Actions for writes. You still need Route Handlers for webhooks, mobile or public APIs, file downloads and OAuth callbacks.
Can Server Actions handle GET requests? No. Server Actions are invoked with POST requests. Use Server Components or Route Handlers for reading data.
Are Server Actions secure? They run only on the server, but each one is reachable as an endpoint, so you must authenticate, authorise and validate input in every action.
Can a mobile app call a Server Action? It is not designed for that. Build Route Handlers (or a separate API) for mobile and third-party clients.
Looking to optimise your Next.js application?
Vibidsoft Pvt Ltd builds and modernises Next.js applications, including App Router migrations, Server Components, secure APIs and performance tuning. See our React and Next.js development services or contact us.
