Server Components in Depth
A version-qualified path through the Next.js App Router: server components, client boundaries, data fetching, caching layers, streaming, error handling, proxy, images, performance, testing and deployment, with the Next.js 15 and 16 behavior changes called out where they matter.
What you will be able to do
#- Trace what executes on the server versus the browser for a given component tree.
- Write async server components that fetch data close to where it renders.
- State what can cross the server to client boundary and what cannot.
Overview
#A third component type
React Server Components are not SSR. Server components render once on the server into a serialized description of the UI; the result is sent as data, not as running code. They never re-render in the browser, never hydrate, and cannot use state or effects. Client components are the familiar kind: they ship JavaScript, hydrate, and can use hooks. In the App Router the server kind is the default.
// app/products/page.tsx - a server component, no directive needed
export default async function ProductsPage() {
const products = await db.product.findMany();
return <ProductGrid products={products} />; // ProductGrid may be a client component
}
Async components are ordinary: an async server component awaits its data before returning UI, and React streams the resolved output.
The boundary is an API contract
Props crossing from a server component into a client component must be serializable: strings, numbers, plain objects, arrays, Dates, Maps and Sets work. Functions, class instances with methods, and server-only handles like database connections do not. If you find yourself wanting to pass an onClick into a server component, the design is wrong; either the handler belongs in a client component, or the server component should be passed as children so it never crosses the boundary.
Composition beats lifting
The strongest server component pattern is passing server-rendered content through a client component as children or slot props:
// server component
import { ClientTabs } from './client-tabs';
export default async function Page() {
const docs = await getDocs();
return <ClientTabs tabs={tabMetadata}>{<DocList docs={docs} />}</ClientTabs>;
}
ClientTabs is interactive, while <DocList> stays a server component rendered on the server and handed through as an already-rendered element. The interactivity island stays small.
Secrets and side effects
Because server components never ship their source, process.env.DATABASE_URL and direct database clients are safe inside them. The reverse rule holds: any value reachable from a client component must be considered public. Mark server-only modules with the server-only package so an accidental client import fails at build time.
Exercises
#Reference answers and expected output are authored, not executed in this browser. Nothing you type is run, and there is no Run or Pass control on this page.
Exercise 1Predict
Which parts of this tree execute in the browser? // app/page.tsx (server) import { Counter } from './counter'; export default async function Page() { const data = await getData(); return <Counter initial={data.count}><Footer/></Counter>; } // counter.tsx has 'use client' and renders children // Footer is a server component
Exercise 2Explain
Explain the difference between server components and server-side rendering, as you would to a teammate who conflates them.
Reveal the reference answer
reference.textSSR is a delivery step: the server renders the current HTML string so the first paint is fast, then the browser downloads and hydrates the same components as client code, after which they re-render like any React app. Server components are an execution location: the component's code never ships to the browser at all, it renders to a serialized tree, and it cannot use hooks or browser APIs. A page can be server-rendered with zero server components (Pages Router) or use server components with streaming (App Router). The practical difference: with RSC the data-fetching and secret-holding logic has a home that costs nothing on the client.Authored expected output: SSR as first-paint delivery versus RSC as code that never ships.
Conflating the two leads to wrong bundle predictions and wrong caching assumptions.
Exercise 3Implement
Write an async server component app/notes/page.tsx that reads a notes list from a local function getNotes() and renders an unordered list. Include the server-only import guard on the data module.
starter.tsx// lib/notes.ts // TODO: mark server-only, export getNotes // app/notes/page.tsx // TODO: async component rendering the listReveal the reference answer
reference.tsx// lib/notes.ts import 'server-only'; export async function getNotes() { return db.note.findMany({ orderBy: { createdAt: 'desc' } }); } // app/notes/page.tsx import { getNotes } from '@/lib/notes'; export default async function NotesPage() { const notes = await getNotes(); return ( <ul> {notes.map((n) => ( <li key={n.id}>{n.title}</li> ))} </ul> ); }Authored expected output: The notes list renders server-side; importing lib/notes from a client component fails the build.
server-only turns an accidental boundary violation into a build error instead of a leaked secret.
Exercise 4Debug
This server component fails with a serialization error at the boundary. Fix it without making the whole page a client component. // app/users/page.tsx (server) import { UserCard } from './user-card'; export default async function Page() { const users = await getUsers(); return <UserCard users={users} onDelete={(id) => deleteUser(id)} />; }
starter.tsx// app/users/page.tsx (server) import { UserCard } from './user-card'; export default async function Page() { const users = await getUsers(); return <UserCard users={users} onDelete={(id) => deleteUser(id)} />; }Reveal the reference answer
reference.tsxThe onDelete function cannot cross into the client component. Move the mutation into a Server Action and pass its imported reference, or remove the prop and have UserCard call the action itself: import { deleteUser } from './actions' inside user-card.tsx and use it in the onClick. Server Action references are serializable endpoints; arbitrary closures are not.Authored expected output: UserCard renders and deletion works through the Server Action.
Functions are the most common accidental boundary violation; Server Actions are the sanctioned way to hand the client a callable.
Exercise 5Test
Write a pure function isSerializableProp(value: unknown): boolean returning true for null, booleans, numbers, strings, plain objects, arrays and Dates, and false for functions, symbols and class instances with methods. Write assertions for: 'hi', 3, { a: [1, 2] }, new Date(), () => {}, Symbol('s'), and new (class { m() {} })().
starter.tsexport function isSerializableProp(value: unknown): boolean { // TODO }Reveal the reference answer
reference.tsexport function isSerializableProp(value: unknown): boolean { if (value === null) return true; const t = typeof value; if (t === 'string' || t === 'number' || t === 'boolean') return true; if (t === 'function' || t === 'symbol' || t === 'undefined') return false; if (value instanceof Date) return true; if (Array.isArray(value)) return value.every(isSerializableProp); if (t === 'object') { const proto = Object.getPrototypeOf(value); if (proto === Object.prototype || proto === null) { return Object.values(value as object).every(isSerializableProp); } return false; } return false; } // Class instances fail the prototype check, which is the approximation React's serializer makes in spirit.Authored expected output: All seven assertions behave as specified.
The boundary rule becomes a testable predicate instead of a vague feeling.
Exercise 6Interview
An interviewer asks: when do you reach for a client component in an App Router codebase, and how do you keep the islands small? Give your decision procedure.
Reveal the reference answer
reference.textA strong procedure: start server-first and add a client boundary only when a leaf needs browser capabilities: local state, event handlers, effects, browser APIs, or third-party widgets that require them. Keep the boundary as deep as possible: instead of marking a page client-side to make one button interactive, extract the button, and pass server-rendered content into it as children or slot props so the data-heavy parts never ship. Push state down and pass serializable data in. If a library demands hooks, wrap only the library. Finish with the audit habit: check what a 'use client' file imports, because the whole import graph joins the bundle.Authored expected output: Server-first default, leaf-level boundaries, children and slot composition, import-graph awareness.
The procedure answer demonstrates judgment rather than slogan recall.
Common misconceptions
#- Server components are just SSR. SSR renders HTML for the first paint; RSC output is a serialized tree that never runs in the browser and needs no hydration for its own logic.
- You can pass functions as props to client components. Functions are not serializable across the server to client boundary; event handlers must live inside client components.
- Async components are a Next.js invention. They are a React Server Components feature that Next.js hosts.
- Adding 'use client' to a file makes only that file client-side. The directive marks the entry of a client graph: everything it imports also ships to the browser.
Follow-up practice
#- nextjs-client-boundaries
- nextjs-data-fetching
Your progress
#Answers, drafts and completion marks are stored only in this browser. Nothing is sent to a server and no account is used.
Verification: authored. Answers and outputs on this page are authored reference material, not executed results.