Alle artikler
22. november 2026

Next.js Server Components vs Client Components: where to draw the line, 2026

Keep Next.js components on the server by default and add a Client Component only for state, effects, event handlers or browser APIs.

Person typing code on a laptop beside a window, a scene for deciding server and client components in Next.js

In the Next.js App Router, make every component a Server Component unless it needs state, effects, event handlers or browser APIs. Then mark only that interactive piece with 'use client' and keep it as small as you can. Everything a client file imports ships to the browser as JavaScript, so the line you draw decides how much JavaScript your users download.

Here is where that line sits, what crosses it, and the mistakes that push it too high in the tree. It follows the Next.js documentation on Server and Client Components, last updated on October 5, 2026.

Server by default, client when the browser is needed

In the App Router, layouts and pages are Server Components by default. They fetch data and render UI on the server, can cache the result, and stream it to the client. A Client Component is something you add on purpose.

Node map diagram with three parts: what is cached, where and for how long
Node map: Server by default, client when the browser is needed.

What belongs on the server

Use Server Components for work that does not need the browser. The Next.js docs list four reasons:

  • Fetching data from databases or APIs close to the source.
  • Using API keys, tokens and other secrets without exposing them to the client.
  • Reducing the JavaScript sent to the browser.
  • Improving First Contentful Paint (FCP) and streaming content progressively.

A Server Component can also decide what gets cached. We cover that in Next.js caching layers: what is cached, where and for how long.

What forces a Client Component

Reach for a Client Component when the code needs one of these:

  • State and event handlers such as onClick and onChange.
  • Lifecycle logic such as useEffect.
  • Browser-only APIs such as localStorage, window or navigator.geolocation.
  • Custom hooks that depend on any of the above.

If none of those apply, leave it on the server. A product description, a table of prices or a list of posts has no reason to be client code.

How the use client boundary spreads

The 'use client' directive declares an entry point for components that render on the client. It defines the boundary between the server and client module graphs.

Where the directive goes

Per the use client reference, the directive goes at the top of the file, before any imports. It applies to the entire module and cannot be placed inside a component or function.

'use client'

import { useState } from 'react'

export default function Counter() {
  const [count, setCount] = useState(0)
  return <button onClick={() => setCount(count + 1)}>{count}</button>
}

What it pulls into the bundle

Once a file is marked, all of its imports and the components it directly renders are included in the client bundle. You do not need the directive on every file inside a client subtree. You need it at the entry, meaning the files whose components a Server Component renders directly.

That is why placement matters. If a layout is a Server Component that imports a Logo (server) and a Search (client), only Search carries the directive and only Search and its imports ship as client code. Put 'use client' on the layout instead and the whole layout joins the bundle.

What crosses the line: code, props and children

The Server and Client boundary guide reduces the model to two rules. Code crosses through imports. Data crosses through props.

Props must be serializable

Props that go from a Server Component to a Client Component must be in a format React can serialize. A function is not. Passing an onClick handler from a Server Component to a Client Component throws.

Three cases are worth knowing:

  • Server Functions marked with 'use server' cross as references, not as function bodies. A <form> can submit through a Server Function passed to its action prop with no Client Component involved.
  • A rendered React element is serializable, so it can cross.
  • A pending promise can be passed as a prop. The Client Component reads it with React's use, and the nearest Suspense boundary shows its fallback until the promise resolves.

One edge case: static members of compound components do not survive the crossing. A Server Component that imports a Client Component gets a client reference, so Menu.Item is undefined and React throws "Element type is invalid." Use named exports instead of static properties.

Pass server content as children

A Client Component does not have to pull in everything nested under it. You can pass a Server Component to it as children or another prop. The server renders that component ahead of time and hands the result to the Client Component as rendered output.

// app/page.tsx (Server Component)
<Modal title={<div>Your cart</div>}>
  <Cart />
</Modal>

Here Modal is a Client Component with useState for visibility, and Cart is a Server Component that fetches data. Modal receives the rendered output of Cart but never its code. The guide explains why it works: the owner of Cart, the component whose source contains the JSX, is Page, which is a Server Component. Modal is only its parent in the rendered tree.

This is the main pattern for small bundles. Wrap the interactive piece in a thin client component and pass server content into it.

Providers and third-party components

Two situations catch teams out.

Context providers. React context is not supported in Server Components. Create a Client Component that accepts children and renders the provider, then import it into a Server Component such as the root layout. The docs add a placement tip: render providers as deep in the tree as possible. Wrapping only {children} instead of the whole <html> document helps Next.js optimize the static parts of Server Components.

Third-party components. A package whose component uses useState but whose module lacks "use client" works inside a Client Component and errors when used directly in a Server Component. Next.js cannot tell it needs the client. The fix is a one-line wrapper in your own client file that re-exports the component. Library authors can add "use client" to entry points that rely on client-only features so users do not need wrappers.

Keep secrets on the server side of the line

Modules can be shared between server and client code, so server-only code can be imported into the client by accident. Next.js limits the damage: only environment variables prefixed with NEXT_PUBLIC_ are included in the client bundle, and unprefixed ones are replaced with an empty string. The code runs but does not work as expected.

To turn that silent failure into a build error, add import 'server-only' at the top of the server-only module. Importing it into a Client Component then fails at build time. The client-only package is the counterpart for modules that touch window. For a worked example of secrets and sessions on the server, see Supabase auth in Next.js: safe server-side sessions.

What the boundary does to your JavaScript budget

Client Components still render on the server. Next.js uses the RSC Payload and the Client Components to prerender HTML, so the first load shows a fast, non-interactive preview. Then JavaScript hydrates the Client Components and attaches event handlers. On later navigations the RSC Payload is prefetched and cached, and Client Components render entirely on the client.

Two things follow. More client code means more JavaScript to download and hydrate, which feeds directly into responsiveness metrics such as Interaction to Next Paint. And content that appears only after a user interaction does not exist in the HTML that a crawler without JavaScript sees. Keep indexable content in Server Components.

Conclusion

Start every component on the server. Move to the client only for state, effects, handlers or browser APIs, and then wrap just that piece. Pass server-rendered content in as children, keep providers deep, and guard secrets with server-only. If you are deciding how to structure a Next.js build, our web development team works through these tradeoffs on real projects.

Frequently asked questions

Are Server Components the default in the Next.js App Router?

Yes. Layouts and pages are Server Components by default, and you opt into a Client Component by adding 'use client' at the top of a file.

Do I need 'use client' on every client component file?

No. The directive is only needed at the entry of a client subtree, and everything that file imports joins the client bundle.

Can a Client Component render a Server Component?

Not by importing it. You can pass a Server Component to a Client Component as children or another prop, and it renders on the server.

Why does passing onClick from a Server Component throw?

Props must be serializable, and a function is not. Server Functions are the exception because they cross as references.

Do Client Components render on the server too?

Yes. They are prerendered to HTML on the server and then hydrated in the browser.