Where to put 'use client' in Next.js so you ship less JavaScript
Guide/ 12 min read
What the 'use client' boundary pulls into the browser bundle, why marking a whole page costs you, and how to move it down to the interactive leaf, measured.
- React
- Next.js
- Performance
In this article (9)
- What the 'use client' boundary is
- What becomes client code: the file and its imports
- Why a boundary at the top ships too much JavaScript
- The fix: move the boundary down to the interactive leaf
- Example: a product page, server vs. client
- How to measure it with the bundle analyzer
- Common mistakes
- Frequently asked questions
- Checklist
The 'use client' directive in Next.js marks a boundary: the file that carries it and everything it imports become client code and ship to the browser. So it does not belong at the top of a page. It belongs on the smallest interactive component, like the add-to-cart button, and the rest stays on the server.
This guide covers what lands in the browser bundle when you mark a file, why marking a whole page sends extra JavaScript and how to move the boundary down on a product page, with numbers from a local demo on Next 15.5.23. If the component you move uses effects, also read when you do not need useEffect for derived state, and before you move anything to the client, read why secrets never go in NEXT_PUBLIC_ variables.
What the 'use client' boundary is
In the App Router every component is a Server Component until you say otherwise. Pages and layouts render on the server, where they can fetch data close to the source and use API keys without exposing them. When a component needs interactivity or browser APIs, the Next.js guide to Server and Client Components has you mark it with "use client".
Per the React reference, the directive goes at the very top of the file, above any imports (comments are fine), in single or double quotes, never backticks. What it marks is not a single component but a boundary in the module dependency tree, not the render tree. Whatever does not cross it stays on the server; the marked file and everything hanging off it is on the client side.
One detail trips a lot of people up: a Client Component does not render only in the browser. Next.js also prerenders it to HTML on the server, then ships its JavaScript to hydrate it, which means wiring the event handlers to HTML that is already on screen. What "use client" changes is that the code travels to the browser and runs there.
The Next.js guide sums up when you need it:
| You need 'use client' when the component uses… | Store example |
|---|---|
State and event handlers (useState, onClick, onChange) | Size picker, add-to-cart button |
Effects and lifecycle (useEffect) | A countdown that updates itself |
Browser APIs (localStorage, window, geolocation) | Remembering the last product viewed |
| Custom hooks built on the above | A stateful useCart() |
A component that only takes data and renders HTML (title, price, description, reviews, gallery) does not need it.
What becomes client code: the file and its imports
React is blunt about it: 'use client' marks the module and its transitive dependencies as client code. Not just components: helpers, data and libraries too. Everything in that module subtree is sent to the browser and runs there.
The Next.js guide says the same thing from the bundle side: once a file is marked, its imports and the components it renders directly are included in the client bundle, so you do not repeat the directive on every child. When a 'use client' module is imported from another client module, the directive has no effect.
Scroll the figure sideways to see all of it
There is one exception, and the whole pattern rests on it: Server Components you pass as children or other props are not part of the client graph. The Client Component never imports them; it receives them already rendered by the server. That is why, in the figure, Reviews stays on the server even though it sits inside Collapsible.
Why a boundary at the top ships too much JavaScript
The usual story: the product page gets a button with useState, the build fails with "This React Hook only works in a Client Component. To fix, mark the file (or its parent) with the "use client" directive", and the quick fix is "use client" on line 1 of page.jsx. It works. But the page is now the root of the client graph, and so is everything it imports:
- The validation library for the product data (in the demo, zod: 12.8 kB gzipped).
- The whole data module. In our build, the review text showed up inside the page's JavaScript chunk.
- Components that only render HTML: gallery, product info, reviews.
All of that is downloaded, parsed and executed to hydrate, even though the shopper will only touch one button. The page also loses what a Server Component gives you: it can no longer be an async component that reads data on the server, and it cannot use keys without exposing them. The Next.js docs make it an explicit recommendation: to reduce client JavaScript, put 'use client' on the interactive components instead of marking large parts of your UI.
The fix: move the boundary down to the interactive leaf
- Find what is interactive. On the product page that is the size picker with its button, and the reviews collapsible. Nothing else uses state or events.
- Move it to its own file with
"use client". That file is the leaf. - Keep the page a Server Component. Read the data there and pass the leaf only what it needs, as serializable props: strings, numbers, arrays and plain objects. Regular functions and class instances cannot cross from the server.
- Use
childrento put server content inside a client component. An accordion, a modal or a collapsible can then control visibility without dragging in what it shows. - Render providers as deep as possible. A context provider is client code; the Next.js guide has it wrap
{children}rather than the whole<html>document. - Wrap third-party components that use hooks but lack the directive in a file of your own with
'use client', and use that wrapper from the server.
Example: a product page, server vs. client
The demo is a running-shoe page with five pieces: gallery, product info, a size picker with a button, a collapsible and reviews. The data comes from lib/product.js, which validates it with zod. This is the top of that file:
import { z } from "zod";
const Product = z.object({
name: z.string().min(1),
price: z.number().int().positive(),
sizes: z.array(z.string()).min(1),
images: z.array(z.string()).min(1),
description: z.string(),
reviews: z.array(
z.object({ author: z.string(), stars: z.number().int().min(1).max(5), text: z.string() }),
),
});
Further down it exports getProduct(), which returns the validated data, and formatPrice(), which formats cents as US dollars with Intl.NumberFormat. This is the version with the boundary at the top, app/top/page.jsx:
"use client";
import { getProduct } from "@/lib/product";
import Gallery from "@/components/Gallery";
import ProductInfo from "@/components/ProductInfo";
import AddToCart from "@/components/AddToCart";
import Collapsible from "@/components/Collapsible";
import Reviews from "@/components/Reviews";
export default function Page() {
const product = getProduct();
return (
<main>
<Gallery images={product.images} name={product.name} />
<ProductInfo product={product} />
<AddToCart sizes={product.sizes} />
<Collapsible title="Reviews">
<Reviews reviews={product.reviews} />
</Collapsible>
</main>
);
}
"use client";on line 1 makespage.jsxthe entry point of the client graph.- All six imports go to the browser with it:
getProductwith zod inside, andGallery,ProductInfoandReviewseven though they have no directive. getProduct()runs the zod validation on every render. With the boundary at the top, that validation also runs in the browser during hydration.- The JSX:
AddToCartgets the sizes andCollapsiblewrapsReviews. HereReviewsis client code too, becausepage.jsximports it.
The interactive parts live in two files with their own directive. The button, components/AddToCart.jsx:
"use client";
import { useState } from "react";
export default function AddToCart({ sizes }) {
const [size, setSize] = useState(sizes[0]);
const [added, setAdded] = useState(false);
return (
<div>
<label>
Size
<select value={size} onChange={(e) => setSize(e.target.value)}>
{sizes.map((s) => (
<option key={s}>{s}</option>
))}
</select>
</label>
<button type="button" onClick={() => setAdded(true)}>
Add to cart
</button>
{added && <p role="status">Size {size} added</p>}
</div>
);
}
- The directive is required: the component uses
useState,onChangeandonClick. - It only receives
sizes, an array of strings. That is serializable, so the server can pass it. - Two pieces of state: the chosen size and whether it was added. In a real store this is where you call your cart action.
role="status"makes screen readers announce the message when it appears.
And the collapsible, components/Collapsible.jsx, which is where the children trick shows:
"use client";
import { useState } from "react";
export default function Collapsible({ title, children }) {
const [open, setOpen] = useState(true);
return (
<section>
<button type="button" aria-expanded={open} onClick={() => setOpen(!open)}>
{title}
</button>
{open && children}
</section>
);
}
- The
openstate decides whether the content shows. childrenarrives already rendered.Collapsibleneither knows nor cares that there are reviews inside.aria-expandedtells assistive technology whether the section is open.
With those two leaves marked, the version with the boundary at the leaves, app/leaf/page.jsx, is exactly the same file minus its first line:
-"use client";
-
import { getProduct } from "@/lib/product";
The page is now a Server Component: it validates the data with zod on the server, renders gallery, info and reviews to HTML, and only the two interactive files ship as JavaScript. Reviews goes into Collapsible as children, already rendered.
Scroll the figure sideways to see all of it
We tested both versions in a browser on next start: in each one the button shows "Size 8 added" and the collapsible hides and shows the reviews again. Same behavior; what changes is how much code arrives.
How to measure it with the bundle analyzer
On Next 15 the quickest measurement is already in your terminal: the table next build prints. The First Load JS column is the JavaScript each route downloads on a first visit (we checked that these are gzipped sizes by recompressing each route's files ourselves). This is an excerpt of the demo's output:
Route (app) Size First Load JS
├ ○ /leaf 487 B 103 kB
├ ○ /leaf-leak 843 B 116 kB
└ ○ /top 1.16 kB 116 kB
+ First Load JS shared by all 103 kB
| Route | Where 'use client' is | First Load JS | zod in the browser? |
|---|---|---|---|
/top | page.jsx | 116 kB | Yes |
/leaf-leak | AddToCartWithPrice.jsx, which imports formatPrice | 116 kB | Yes |
/leaf | AddToCart.jsx and Collapsible.jsx | 103 kB | No |
This is a lab measurement from my local demo (Next 15.5.23 on webpack, React 19.0.0, zod 3.25.76), not a promise about your store. The shared 103 kB is React and Next.js, the same on every route; the 13 kB difference is mostly the zod chunk (56,562 bytes raw, 12.8 kB gzipped) plus the components and the data. On a real page that imports a date library, a rich-text editor or a syntax highlighter, the gap grows with whatever you pull in.
Scroll the figure sideways to see all of it
To find out what is in each chunk without installing anything, search the build output for a string only that library contains. In our demo:
grep -rl "ZodError" .next/static/chunks
.next/static/chunks/231-2ddfea1722ade6a5.js
When you need the full map, the official tool for Next 15 is @next/bundle-analyzer, per the Next.js 15 package bundling guide. Install it with npm i @next/bundle-analyzer and turn it on in next.config.js:
/** @type {import('next').NextConfig} */
const nextConfig = {}
const withBundleAnalyzer = require('@next/bundle-analyzer')({
enabled: process.env.ANALYZE === 'true',
})
module.exports = withBundleAnalyzer(nextConfig)
enableddepends on theANALYZEvariable, so your normal build does not change.withBundleAnalyzerwraps your config; if you already have anextConfig, pass it in as is.- You generate the report with
ANALYZE=true npm run build(in PowerShell,$env:ANALYZE="true"; npm run build), and per the docs it opens three browser tabs.
We did not install it in the demo; the setup above is the documented one. Do not look for a built-in analyzer on Next 15 either: next --help on 15.5.23 has no analyze command. Since Next 16.1 there is next analyze, built on Turbopack, per the current package bundling guide. And if you upgrade to Next 16, note that per the version 16 upgrade guide next build no longer prints the Size and First Load JS columns; measure with next analyze or Lighthouse there.
Common mistakes
- Marking the page or the layout to make one button work. It works, and it ships everything that page imports. Move the button to its own file.
- Importing a helper from a heavy module. In
/leaf-leakthe button importedformatPricefromlib/product.jsto show the price. One import, and zod and the data were back in the browser (116 kB). Keep client-side helpers in their own module with no server dependencies. - Importing a Server Component into a Client Component. Importing it turns it into client code. If it must stay on the server, pass it as
childrenor another prop. - Passing functions or class instances as props from the server. They are not serializable and React throws. Pass data; the exception is Server Functions marked with
'use server'. - Putting the directive below the imports. It must be the first thing in the file; only comments may come before it.
- Wrapping the whole
<html>in a provider. Wrap only{children}, as deep in the tree as you can. - Assuming a Client Component never runs on the server. It is prerendered to HTML too. If you read environment variables there, first read what happens to NEXT_PUBLIC_ and secrets.
Frequently asked questions
Does 'use client' make a component render only in the browser?
No. Next.js prerenders it to HTML on the server and then ships its JavaScript to hydrate it. What the directive decides is that the code goes to the browser and runs there.
Do I need 'use client' on every child component?
No. Everything a marked file imports is already client code. Put it on the leaves you use from Server Components, like AddToCart.
Can I use a Server Component inside a Client Component?
Yes, by passing it as children or another prop from a Server Component. What you cannot do is import it from the client file, because that turns it into client code.
How do I know how much JavaScript I saved?
On Next 15, compare the First Load JS column of next build before and after, and use @next/bundle-analyzer to see what is inside. On Next 16.1 or later, use next analyze.
Checklist
- No page or layout has
'use client'unless all of it needs to. - Every file with the directive uses state, events, effects or browser APIs.
- Client Components get serializable props, not functions or class instances.
- Server content goes into Client Components as
children. - Helpers used on the client do not live in modules with server libraries or data.
- You compared First Load JS before and after, and searched
.next/static/chunksfor heavy libraries.
Moving the boundary down is nearly always deleting one line and moving one component into its own file. If you find a useEffect that only computes data while you are at it, continue with useEffect and derived state; if your page reads environment variables, check NEXT_PUBLIC_ variables and secrets in Next.js. For the wider performance picture, see why store speed is usually an architecture problem and the slow add-to-cart button.
Saw this on Instagram? Comment CLIENTE on the Reel and I'll send you the guide.
Written by

Published October 30, 2026 · updated November 1, 2026