Server and Client Components: Which One Do You Need?
React & Next.js

Server and Client Components: Which One Do You Need?

10 November 20265 min read897 words
Tags#react#nextjs#server-components#performance

In the app router, a component is a server component unless you say otherwise. That one default changes how you think about data, bundles and interactivity, and it is the source of most of the confusion people hit in their first weeks. This post is about the practical question: for this particular piece of the page, which kind do you want, and what does the choice cost?

What a server component is

A server component runs on the server and never ships to the browser. It can read from a database, use a secret key, read a file, and await data directly in the component body. What arrives in the browser is the resulting markup, not the code that produced it.

That has two consequences worth stating plainly. Your data-fetching code and any library it uses stay out of the bundle, which is often a large share of what used to be shipped. And the component cannot use state, effects, event handlers, or anything that depends on the browser, because none of that exists where it runs.

// server component: no directive needed
export default async function Invoices() {
  const rows = await db.invoice.findMany({ take: 20 });
  return <InvoiceTable rows={rows} />;
}

What 'use client' actually does

'use client' at the top of a file marks the boundary where the client part of the tree begins. The component, and everything it imports, is compiled for the browser and shipped.

The word that matters is "everything it imports". Marking a component as client makes every component it imports part of the client tree as well, whether or not those components needed it. One 'use client' near the top of a page is how an entire page ends up in the bundle, which is the most common performance mistake in this model.

Client components still render on the server for the initial HTML. "Client" does not mean "not server-rendered"; it means the code is also sent to the browser so it can hydrate and become interactive.

Where the line belongs

The rule that works in practice: push the boundary down, as far into the tree as the interactivity actually goes.

A page with a data table, a search box and a filter dropdown does not need to be a client component. The page, the layout and the table rows can be server components. The search box is a client component. The dropdown is a client component. The rest of the page never reaches the browser as code.

// page.tsx (server component)
export default async function Page({ searchParams }) {
  const rows = await search(searchParams.q);
  return (
    <>
      <SearchBox />        {/* 'use client' lives in this file */}
      <Results rows={rows} />   {/* server */}
    </>
  );
}

When a client component needs to display something rendered on the server, pass it as children rather than importing it. A server component passed through children to a client component stays a server component; one imported into a client file does not.

What crosses the boundary

Props passed from a server component to a client component are serialised, so they must be serialisable: strings, numbers, plain objects, arrays, dates. Functions, class instances and anything holding a database connection cannot cross. The error message when you try is clear enough, but the design implication is the part to internalise: the client gets data, not capabilities.

Anything you pass is also visible to the user, because it ends up in the payload sent to the browser. Selecting the fields you actually need is not just a performance habit; passing a whole user row to a client component publishes it.

The mistakes that show up in review

  • 'use client' at the top of the page file. Almost always the wrong place. Move it to the leaf that needs state.
  • A provider wrapping everything. Context providers are client components, so wrapping the root layout pulls the tree along. Wrap the smallest subtree that consumes the context.
  • Fetching in an effect out of habit. If the data is needed to render, await it in a server component. The effect version costs an extra round trip and a loading state you then have to design.
  • A component library imported wholesale. Some libraries mark their entry point as client, which makes every import from them a client import. Import from the specific module where the library supports it.
  • Secrets in a file that becomes client. Anything imported into a client component ships. Keep server-only code in files marked with the server-only guard so a mistaken import fails the build instead of leaking.

How to check what you shipped

Do not reason about it, measure it. The build output lists the JavaScript per route, and a route that grew by a hundred kilobytes after a small change is telling you a boundary moved. In the browser, the React developer tools show which components hydrated. For the page as a whole, the network panel with JavaScript disabled shows what the server actually produced, which is also a fair approximation of what a crawler without a script engine sees.

The short version

Default to server. Mark a component as client when it needs state, an effect, an event handler or a browser API, and mark the smallest component that needs it. Pass data down, pass server-rendered content through children, and check the bundle after anything that felt like a small refactor.

Comments

No comments yet. Be the first to share your thoughts.

Server vs Client Components in Next.js: A Practical Guide