Performance: Response modification
Modifying HTTP responses affects page load speed by potentially delaying the first byte and can impact caching efficiency.
Jump into concepts and practice - no test required
export async function GET(request) { const data = await fetch('https://api.example.com/data'); const json = await data.json(); // Use Response constructor with headers directly return new Response(JSON.stringify(json), { headers: { 'X-Custom-Header': 'value' } }); }
export async function GET(request) { const data = await fetch('https://api.example.com/data'); const json = await data.json(); // Synchronous blocking header modification const headers = new Headers(); headers.append('X-Custom-Header', 'value'); return new Response(JSON.stringify(json), { headers }); }
| Pattern | DOM Operations | Reflows | Paint Cost | Verdict |
|---|---|---|---|---|
| Synchronous body modification in middleware | N/A | N/A | Blocks streaming, delays paint | [X] Bad |
| Header-only modification in middleware | N/A | N/A | No blocking, fast paint | [OK] Good |
| Synchronous header creation after fetch | N/A | N/A | Blocks first byte, delays LCP | [X] Bad |
| Direct header setting in Response constructor | N/A | N/A | Fast first byte, improves LCP | [OK] Good |
export default function handler(req, res) {
res.status(404).json({ error: 'Not found' });
}export default function handler(req, res) {
res.status(200);
res.json({ message: 'Hello' });
res.setHeader('X-Test', 'value');
}export default function handler(req, res) {
const isAuth = req.headers.authorization === 'secret-token';
// Add caching headers only if authenticated
???
res.status(200).json({ data: 'Secure data' });
}