Performance: Response formatting
Response formatting affects how quickly the server sends data and how efficiently the browser processes it, impacting page load speed and interaction readiness.
Jump into concepts and practice - no test required
export default function handler(req, res) { const data = { message: 'Hello World', time: new Date().toISOString() }; res.json(data); }
export default function handler(req, res) { const data = { message: 'Hello World', time: new Date() }; res.setHeader('Content-Type', 'application/json'); res.send(JSON.stringify(data)); }
| Pattern | DOM Operations | Reflows | Paint Cost | Verdict |
|---|---|---|---|---|
| Manual JSON.stringify with extra data | N/A | 0 | Low but delayed by parsing | [X] Bad |
| res.json with minimal data | N/A | 0 | Low and fast parsing | [OK] Good |
Content-Type header in a Next.js API response?Content-Typeexport async function GET() {
const data = { message: 'Hello' };
return new Response(JSON.stringify(data), {
status: 201,
headers: { 'Content-Type': 'application/json' }
});
}export async function POST() {
const data = { success: true };
return new Response(data, {
status: 200,
headers: { 'Content-Type': 'application/json' }
});
}