What is the component tree boundary between server and client?

The boundary is the first module marked "use client". Everything above it in the import graph runs on the server; that module and everything it imports is bundled for the client. Server Components can still appear inside the client part of the tree, but only when passed in as props such as children.

Two trees to keep apart

  • Module graph (imports): decides which code ships to the browser. "use client" cuts the graph: a Client Component's imports are client code.
  • Render tree (JSX nesting): decides what renders inside what. A Server Component can be rendered inside a Client Component if a Server Component parent created it and passed it down.

What crosses the boundary

  • Props from server to client must be serializable: data, JSX, promises, Server Functions.
  • Children/slots let server-rendered UI sit inside client UI without becoming client code.
  • Server Functions let client code call back into the server.

Placement strategy

  • Keep the boundary at the leaves: an interactive button, a form, a chart.
  • Wrap non-boundary third-party components (libraries using hooks without "use client") in your own small client file.
  • Shared utilities without hooks or browser APIs can be imported on both sides; they get duplicated into both bundles.

Guardrails

  • import "server-only" in modules with secrets or database access: importing them from client code fails the build.
  • import "client-only" for modules that touch window.
  • Use the Next.js DevTools and the bundle analyzer (next analyze) to see what crossed the boundary.

Pitfall

"use client" in a layout or page makes every component that module imports a client component, even ones that did not need it.

Code example

tsx
1// app/page.tsx (server)
2import { Tabs } from "./tabs"; // "use client" boundary
3import { ServerStats } from "./server-stats"; // async Server Component
4
5export default function Page() {
6 return (
7 // ServerStats is created here on the server and passed through
8 <Tabs stats={<ServerStats />}>
9 <p>Rendered on the server, displayed inside a client component</p>
10 </Tabs>
11 );
12}
13
14// app/tabs.tsx
15// "use client";
16// import { useState } from "react";
17// import { formatDate } from "@/lib/format"; // imported here => bundled for the client
18// export function Tabs({ stats, children }: { stats: React.ReactNode; children: React.ReactNode }) {
19// const [tab, setTab] = useState<"a" | "b">("a");
20// return (
21// <div>
22// <button onClick={() => setTab("a")}>Info</button>
23// <button onClick={() => setTab("b")}>Stats</button>
24// {tab === "a" ? children : stats}
25// </div>
26// );
27// }

Practice this question

Open it in the study app to track progress, take notes, listen to the answer and work through the rest of the Next.js set.

Open in study app