3. Server Components & Rendering
Server Components & Rendering
Every component you write in the app/ directory is a Server Component by default. It runs on the server, never ships its code to the browser, and can talk directly to a database or filesystem. This is the single biggest departure from traditional React, so this module is worth taking slowly.
What Actually Happens on the Server
When a request comes in, Next.js runs your Server Component's code on the server, renders it to HTML, and sends that HTML to the browser. The component's JavaScript itself — its imports, its logic — never reaches the client. Only the rendered output does.
// app/products/page.tsx import { db } from '@/lib/db'; export default async function ProductsPage() { const products = await db.product.findMany(); return ( <ul> {products.map((p) => ( <li key={p.id}>{p.name}</li> ))} </ul> ); }
Notice the component is async. That's allowed because it runs on the server — you can await a database call or fetch directly inside the component body, no extra hook required.
Why This Matters
- Smaller bundles — server-only code (and its dependencies) never gets sent to the browser.
- Direct data access — no separate API layer needed just to read from a database.
- Secrets stay secret — API keys and credentials used here are never exposed to users.
- Faster first paint — the browser receives ready-made HTML instead of an empty shell waiting on JS.
The Limits of Server Components
Server Components can't use browser-only features — no useState, no useEffect, no event handlers like onClick. They render once on the server and produce static HTML; they have no lifecycle in the browser. For anything interactive, you need a Client Component, which is what the next module covers.
onClick, useState, or any browser API, it can't be a Server Component — full stop.
// THIS WILL FAIL (Server Components can't use hooks or browser APIs) export default function InvalidServerComponent() { const [count, setCount] = useState(0); // Error! const width = window.innerWidth; // Error! return <div>{count}</div>; }
Static vs. Dynamic Rendering
Next.js decides, per route, whether to render at build time (Static Rendering) or on every request (Dynamic Rendering). Static is the default and the fastest — the HTML is generated once and reused for every visitor. A route becomes dynamic automatically the moment it reads something request-specific, like cookies, headers, or search params.
import { cookies } from 'next/headers'; export default async function DashboardPage() { const theme = cookies().get('theme'); // Reading cookies() makes this route dynamic automatically return <div>Theme: {theme?.value}</div>; }
You rarely need to declare this manually — Next.js infers it from what your code actually does.
Streaming with Suspense
Slow data shouldn't block a whole page. Wrap a slow section in <Suspense>, and Next.js streams the rest of the page immediately, filling in that section the moment its data is ready.
import { Suspense } from 'react'; export default function Page() { return ( <div> <h1>Dashboard</h1> <Suspense fallback={<p>Loading stats...</p>}> <SlowStats /> </Suspense> </div> ); }
Where You're Headed
Server Components handle rendering and data — but apps need buttons, forms, and state too. Next module: Client Components, and exactly where to draw the line between the two.
Deep Dive Reference: Server Components
To truly master Next.js, you must understand the exact rules, edge cases, and parameters of React Server Components (RSC).
Strict Rules of Server Components
- No State:
useStateanduseReducerwill throw errors. - No Lifecycles:
useEffectanduseLayoutEffectare strictly prohibited. - No Browser APIs: You cannot access
window,document, orlocalStorage. - Async by Default: Server components can be
asyncfunctions, allowing you to useawaitdirectly in the component body.
Edge Cases & Gotchas
If you import a module that relies on browser-only APIs (like a slider library), it will crash the server render unless you dynamically import it with ssr: false or wrap it in a Client Component.
// Next.js explicitly supports this pattern: import dynamic from 'next/dynamic'; const ClientSlider = dynamic(() => import('./Slider'), { ssr: false });
Serialization Boundary
When passing props from a Server Component to a Client Component, the props must be serializable. Functions, Dates, Maps, and Sets cannot be passed directly. They must be converted to strings or plain objects first.
Knowledge Check
Ready to test your understanding of 3. Server Components & Rendering?