Performance: Server component database queries
HIGH IMPACT
This affects the page load speed by controlling when and how data is fetched from the database during server rendering.
import { fetchData } from '@/lib/db'; export default async function Page() { const data = await fetchData(); 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 fetch in React useEffect | Adds extra DOM nodes for loading state | Triggers multiple reflows during loading and data update | Higher paint cost due to incremental updates | [X] Bad |
| Server component async data fetch | Minimal DOM nodes, fully rendered HTML | Single reflow on initial load | Lower paint cost with ready content | [OK] Good |