Performance: Why API routes serve data endpoints
This affects how fast data loads on the page and how responsive the app feels when fetching or sending data.
export const GET = async () => { const data = await fetchExternalData(); return new Response(JSON.stringify(data), { headers: { 'Content-Type': 'application/json' } }); }; // API route serves data endpoint separately, page fetches from this route asynchronously.
export async function load() { const res = await fetch('https://external-api.com/data'); const data = await res.json(); return { data }; } // This fetches data directly in the page load, blocking rendering until data arrives.
| Pattern | DOM Operations | Reflows | Paint Cost | Verdict |
|---|---|---|---|---|
| Direct fetch in page load | Minimal | 1 (delayed) | High (blocks paint) | [X] Bad |
| API route serving data endpoint | Minimal | 1 (async after render) | Low (non-blocking) | [OK] Good |
export async function GET() {
return new Response(JSON.stringify({ success: true, count: 5 }), {
headers: { 'Content-Type': 'application/json' }
});
}export function GET() {
return { body: { message: 'Hi' } };
}const users = [{ id: 1, name: 'Alice' }, { id: 2, name: 'Bob' }];Pass the test and this pattern moves unknown