GresBoxGresBox
← All articles

Where to put use client in the App Router

One directive at the top of a page file can add 70 kB of JavaScript to a route that needed none. Finding the real boundary is usually a five-minute change.

7 min read

  • Performance
  • Next.js
  • React

The App Router renders on the server by default. You opt out with a use client directive, and the moment you do, that component and everything it imports gets compiled into the client bundle and shipped to the browser.

That is fine when the thing you marked is a dropdown. It is expensive when the thing you marked is a page, because a page imports its sections, and those sections import their icons, and now a route that displays static text is downloading an animation library.

How to tell if you have the problem

Run a production build and read the route table. Next prints First Load JS per route, and the shape of the answer is usually obvious:

Route                     Size     First Load JS
/contact                  3.4 kB   171 kB
/privacy-policy           136 B    102 kB

Both pages render text and links. One of them is a client component and the other is not, and the 69 kB gap between them is the entire cost of the directive. The shared baseline, 102 kB here, is React and the framework runtime; that part you do not control.

If every route in your table sits well above the shared baseline, the boundary is almost certainly in the wrong place.

Finding the real boundary

Ask one question of each component: does this need something that only exists in a browser? That is a short list.

  • State that changes in response to a user: useState, useReducer
  • Effects and subscriptions: useEffect, event listeners, timers
  • Browser APIs: window, document, localStorage, IntersectionObserver
  • Event handlers: onClick, onChange, onSubmit
  • Context providers that hold changing values

Everything else can render on the server, including components that receive data, map over it and return markup. Rendering a list is not interactivity.

The pattern: a server wrapper around a client leaf

Say you have a page that is marked client only because a single accordion needs to open and close. Split it. The page stays a Server Component, which also means it can export metadata again, and only the accordion carries the directive.

// app/faqs/page.tsx  —  Server Component
import type { Metadata } from "next"
import { faqsPage } from "@/content/pages/faqs"
import FaqAccordion from "./FaqAccordion"

export const metadata: Metadata = faqsPage.meta

export default function FaqsPage() {
  const { heading, items } = faqsPage.faq
  return (
    <main>
      <h1>{heading}</h1>
      <FaqAccordion items={items} />
    </main>
  )
}
// app/faqs/FaqAccordion.tsx  —  the only client code on the route
"use client"

export default function FaqAccordion({ items }) {
  const [open, setOpen] = useState<string | null>(null)
  // ...
}

The heading, the lead paragraph and the question text all render on the server. Only the open and close behaviour ships. A crawler that does not execute JavaScript still sees every answer, which matters more than it used to now that answer engines fetch pages without running scripts.

A detail that catches people out

A client component can render a Server Component, but only if it receives it as a prop or as children. It cannot import one. This works:

// Server Component
<ClientShell>
  <ExpensiveServerRenderedThing />
</ClientShell>

The children are rendered on the server and passed through as already-rendered output. The client shell can animate them, position them or toggle them without any of their code entering the bundle. That single trick handles most animation wrappers, which is the usual reason a page got marked client in the first place.

Watch for the layout

A component mounted in the root layout is on every route. If your header is a client component because of a scroll listener and a mobile menu, then every page on the site ships the header, its icons and whatever else it imports, no matter how static that page is.

This is worth checking first, because it is the one place where a single fix improves every route at once.

What to do on Monday

  1. 1.Run a production build and note First Load JS for your simplest text-only route. That is your realistic floor.
  2. 2.List every route more than about 30 kB above it.
  3. 3.In each, find the actual interactive element. It is usually one form, one menu or one animation wrapper.
  4. 4.Move the directive down to that element and pass it data as props.
  5. 5.Rebuild and compare. Also confirm the page can now export metadata, because that is often the bigger win.
The goal is not zero client JavaScript. It is that the JavaScript you ship corresponds to something the user can actually interact with.

Working on something like this?

If this article describes a problem you are living with, we can probably help you fix it. Tell us what you are running into.