Performance: Server component restrictions
This affects the initial page load speed and server response time by limiting what code runs on the server versus the client.
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 data fetching in component | Many (due to hydration) | Multiple (due to delayed content) | High (JS parsing and execution) | [X] Bad |
| Server component with data fetching | Minimal (static HTML) | Single (initial paint) | Low (HTML only) | [OK] Good |
window.window.alert() requires a browser environment, so it is not allowed in server components.useState hook inside a Next.js server component?useState require a browser environment and are only valid in client components.useState in server components causes a runtime error because state management is not supported there.import { useEffect } from 'react';
export default function Page() {
useEffect(() => { console.log('Hello'); });
return <div>Welcome</div>;
}useEffect run only in client components because they depend on browser lifecycle.useEffect in a server component causes an error since server components do not support client hooks.fetch directly on the server to securely get data before rendering.window.fetch does not exist on server; file system access is server-only.Pass the test and this pattern moves unknown