Performance: Response modification
MEDIUM IMPACT
Modifying HTTP responses affects page load speed by potentially delaying the first byte and can impact caching efficiency.
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 |