Performance: Request parsing in route handlers
This affects server response time and how quickly the page can start rendering by controlling how fast the request data is processed.
Jump into concepts and practice - no test required
export async function POST(req) { const body = await req.json(); // offload heavy processing to background task or optimize logic return new Response(JSON.stringify({ message: 'Done' }), { status: 200 }); }
export async function POST(req) { const body = await req.json(); // heavy synchronous processing here return new Response(JSON.stringify({ message: 'Done' }), { status: 200 }); }
| Pattern | DOM Operations | Reflows | Paint Cost | Verdict |
|---|---|---|---|---|
| Heavy synchronous parsing in route handler | 0 (server-side) | 0 | 0 | [X] Bad |
| Efficient async parsing with minimal processing | 0 (server-side) | 0 | 0 | [OK] Good |
request.json() method parses the request body as JSON.request.formData().const data = await request.formData(); to properly parse form data.export async function POST(request) {
const data = await request.json();
console.log(data.name);
return new Response('OK');
}request.json() method converts the JSON string into a JavaScript object.data.name will be "Alice".export async function POST(request) {
const data = request.json();
return new Response(JSON.stringify(data));
}request.json() method returns a promise and must be awaited.await, data is a promise, not the parsed object, causing incorrect response.await request.json() for 'application/json' and await request.formData() for 'multipart/form-data' or 'application/x-www-form-urlencoded'.