Performance: Fetch in server components
MEDIUM IMPACT
Fetching data in server components affects the initial page load speed and server response time.
export default async function Page() { const res = await fetch('https://example.com/api/data'); const data = await res.json(); return <div>{data.message}</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.message}</div>; }
| Pattern | DOM Operations | Reflows | Paint Cost | Verdict |
|---|---|---|---|---|
| Client fetch in useEffect | Low initially, then updates DOM | Multiple reflows due to loading state and data update | Higher paint cost due to delayed content | [X] Bad |
| Server fetch in server component | DOM fully rendered once | Single reflow on initial paint | Lower paint cost with immediate content | [OK] Good |