Server Components vs Client Components
The App Router gives you two types of React components, each optimized for different environments.When to use each
- Server Components
- Client Components
Use Server Components when you need to:
- Fetch data from databases or APIs close to the source
- Use API keys, tokens, and secrets without exposing them to the client
- Reduce the amount of JavaScript sent to the browser
- Improve First Contentful Paint (FCP) and stream content progressively
Creating a Client Component
"use client" declares a boundary between the Server and Client module graphs. Once a file is marked with "use client", all its imports and child components are considered part of the client bundle.
Composing Server and Client Components
A common pattern is to fetch data in a Server Component and pass it as props to an interactive Client Component:Passing Server Components as props
You can pass Server Components as children or props to Client Components. This allows you to nest server-rendered UI inside client-side state:<Cart />) are rendered on the server ahead of time. The RSC Payload contains references to where Client Components should render in the tree.
How rendering works
On the server
Next.js uses React’s APIs to orchestrate rendering, split by individual route segments (layouts and pages):- Server Components are rendered into a special data format called the React Server Component Payload (RSC Payload)
- Client Components and the RSC Payload are used to prerender HTML
The RSC Payload is a compact binary representation of the rendered React Server Components tree. It contains the rendered result of Server Components, placeholders for Client Components with references to their JavaScript files, and any props passed from Server to Client Components.
On the client
On the client:- HTML is used to immediately show a fast non-interactive preview of the route
- RSC Payload is used to reconcile the Client and Server Component trees
- JavaScript hydrates Client Components and makes the application interactive
Subsequent navigations
On subsequent navigations, the RSC Payload is prefetched and cached for instant navigation. Client Components are rendered entirely on the client, without the server-rendered HTML.Rendering strategies
Prerendering (static)
Prerendering happens at build time or during revalidation. The result is cached and served to all users. This is the default behavior for routes that don’t use runtime data.Dynamic rendering
Dynamic rendering happens at request time when a route accesses runtime APIs likecookies(), headers(), or searchParams.
Partial Prerendering (PPR)
With Cache Components enabled, Next.js uses Partial Prerendering by default. This generates a static shell at build time while allowing specific parts of the page to stream in at request time.Preventing environment poisoning
JavaScript modules can be shared between Server and Client Components. Use theserver-only package to prevent accidental usage of server-side code on the client:
client-only package works the same way for client-only code.
