Performance: Data fetching in server components
HIGH IMPACT
This affects the initial page load speed by controlling when and how data is fetched and rendered on the server before sending HTML to the browser.
export default async function Page() { const res = await fetch('https://example.com/api/data'); const data = await res.json(); return <div>{data.title}</div>; }
import React from 'react'; export default function Page() { const [data, setData] = React.useState(null); React.useEffect(() => { fetch('/api/data').then(res => res.json()).then(setData); }, []); if (!data) return <p>Loading...</p>; return <div>{data.title}</div>; }
| Pattern | DOM Operations | Reflows | Paint Cost | Verdict |
|---|---|---|---|---|
| Client-side data fetching with useEffect | Minimal initial DOM, then full update | Multiple reflows due to loading state and data render | Higher paint cost due to incremental updates | [X] Bad |
| Server component data fetching with async/await | Full DOM ready on first paint | Single reflow on initial load | Lower paint cost with complete HTML | [OK] Good |