0
0
NextJSframework~8 mins

API routes vs server actions decision in NextJS - Performance Comparison

Choose your learning style9 modes available
Performance: API routes vs server actions decision
MEDIUM IMPACT
This affects server response time and client-side rendering speed by choosing how backend logic is executed and data is fetched.
Fetching data or performing server logic in Next.js app
NextJS
export async function serverAction() {
  const data = await fetchFromDatabase();
  return data;
}

// Called directly from server components or client with server action support
Server actions run server logic directly without extra HTTP requests, reducing latency and speeding up rendering.
📈 Performance GainEliminates extra network request, reducing LCP by 100-200ms and improving time to interactive
Fetching data or performing server logic in Next.js app
NextJS
export async function GET(request) {
  // API route handler
  const data = await fetchFromDatabase();
  return new Response(JSON.stringify(data));
}

// Client fetches data via fetch('/api/data')
API routes add an extra HTTP request and response cycle, increasing latency and delaying rendering.
📉 Performance CostAdds 1 extra network request, blocking rendering until response arrives, increasing LCP by 100-200ms
Performance Comparison
PatternNetwork RequestsServer ProcessingClient Wait TimeVerdict
API Routes1 extra HTTP requestSeparate server handlerClient waits for response[X] Bad
Server ActionsNo extra HTTP requestInline server logicMinimal client wait[OK] Good
Rendering Pipeline
API routes require a client request to the server, which triggers server processing, response serialization, then client parsing before rendering. Server actions run server code during rendering, skipping the client fetch step.
Server Processing
Network Request
Client Rendering
⚠️ BottleneckNetwork Request latency and client waiting for response
Core Web Vital Affected
LCP
This affects server response time and client-side rendering speed by choosing how backend logic is executed and data is fetched.
Optimization Tips
1Avoid extra HTTP requests to reduce latency and improve LCP.
2Use server actions to run server logic inline during rendering.
3Check Network panel to verify number of requests and their timing.
Performance Quiz - 3 Questions
Test your performance knowledge
What is a main performance drawback of using API routes instead of server actions in Next.js?
AExtra network request adds latency
BServer actions increase bundle size
CAPI routes block client rendering permanently
DServer actions cause layout shifts
DevTools: Network
How to check: Open DevTools, go to Network tab, load page and observe number of requests and their timing
What to look for: Extra API route requests increase load time; server actions show fewer requests and faster content load