Performance: API routes vs server actions decision
This affects server response time and client-side rendering speed by choosing how backend logic is executed and data is fetched.
Jump into concepts and practice - no test required
export async function serverAction() { const data = await fetchFromDatabase(); return data; } // Called directly from server components or client with server action support
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')
| Pattern | Network Requests | Server Processing | Client Wait Time | Verdict |
|---|---|---|---|---|
| API Routes | 1 extra HTTP request | Separate server handler | Client waits for response | [X] Bad |
| Server Actions | No extra HTTP request | Inline server logic | Minimal client wait | [OK] Good |
export async function addNumbers(a, b) {
return a + b;
}export async function handler(req, res) {
res.status(200).json({ message: 'Hello' });
}export default async function handler(req, res).export default async function handler(req, res). -> Option D