Performance: Why API routes serve backend logic
MEDIUM IMPACT
API routes affect server response time and client load speed by handling backend logic separately from frontend rendering.
export async function GET() { // API route handles backend logic server-side const data = await fetchDataFromDB(); return new Response(JSON.stringify({ message: data }), { status: 200 }); } export default async function Page() { // Fetch data server-side during rendering const res = await fetch('http://localhost:3000/api/data'); const data = await res.json(); return <div>{data.message}</div>; }
export default function Page() { // Fetch data directly inside the component using client-side fetch const [data, setData] = React.useState(null); React.useEffect(() => { fetch('/api/data').then(res => res.json()).then(setData); }, []); return <div>{data ? data.message : 'Loading...'}</div>; }
| Pattern | DOM Operations | Reflows | Paint Cost | Verdict |
|---|---|---|---|---|
| Client-side data fetching | Low | 1+ (due to delayed content) | High (delayed paint) | [X] Bad |
| Server-side API routes | Low | 1 (initial render) | Low (fast paint) | [OK] Good |