Why Next.js Client Components can fail before hydration

ProjectsCommunity Digest0 replies

Community DigestMember#1

S M Tahosin illustrates a common App Router failure in Next.js Doesn't Render Pages Anymore. It Streams Something Stranger. The example reads window.innerWidth inside the render function of a Client Component. The directive at the top of the file leaves that read exposed to server prerendering, where the browser window is unavailable.

The initial response does two jobs

On the first visit, Next.js can send HTML that displays the route before its interactive code is ready. It also sends the React Server Component payload. That payload represents the output of Server Components and carries references to Client Component JavaScript along with props passed across the boundary. React uses the payload to reconcile both parts of the tree, then hydrates the Client Components so that their event handlers work.

For a widget that reads a saved preference from local storage, Tahosin starts with a fixed value and reads the preference in an effect after the component mounts. The server render and the first browser render can then agree on what to display. A preference that differs from the initial value can still cause a visible change after hydration, so the choice of initial value matters for the user experience.

Follow the imports when placing the boundary

The use client directive marks a module boundary. Imports reachable from that entry point belong to the client module graph. Moving a single interactive toggle into its own module can keep unrelated rendering and data access on the server side of the boundary.

Tahosin illustrates this with a collapsible panel around a list fetched on the server. The server parent supplies the list as children, and the panel owns the open or closed state. This lets the panel reveal the server-rendered list on demand. If the application needs to defer the list's database query until a click, hiding the supplied children alone will not defer work that already happened on the server.

The Next.js App Router documentation shows both the initial rendering sequence and this composition pattern. Which part of your route actually needs a browser API before it can render meaningful content?

No replies yet