Server and Client Components in Next.js – Rendering, Data Flow, and Bundle Optimization

Next.js combines Server and Client Components to build fast, interactive applications. This article explains how React Server Component Payloads work, how hydration enables interactivity, how to pass data between components, and how to optimize JavaScript bundles using the "use client" directive. It also covers context usage, third-party integration, and environment safety.

Server ComponentClient ComponentRSC Payloaduse client

~3 min read · Updated Oct 25, 2025

Server Rendering in Next.js


Next.js uses React APIs to render components on the server. Server Components are compiled into a special format called the React Server Component Payload (RSC), which includes:


  • The rendered output of Server Components
  • Placeholders for Client Components and references to their JavaScript files
  • Props passed from Server to Client Components

Client Rendering and Hydration


On the client:

  • HTML provides a fast, non-interactive preview
  • The RSC Payload reconciles the component tree
  • JavaScript hydrates Client Components to enable interactivity

Hydration is the process of attaching event handlers to static HTML.


Using Client Components


To define a Client Component, add 'use client' at the top of the file:


// app/ui/counter.tsx
'use client'
import { useState } from 'react'

export default function Counter() {
  const [count, setCount] = useState(0)
  return (
    <div>
      <p>{count} likes</p>
      <button onClick={() => setCount(count + 1)}>Click me</button>
    </div>
  )
}

Reducing JavaScript Bundle Size


Instead of marking entire layouts as Client Components, isolate interactive parts:


// app/layout.tsx
import Search from './search'
import Logo from './logo'

export default function Layout({ children }) {
  return (
    <nav>
      <Logo />
      <Search />
    </nav>
    <main>{children}</main>
  )
}

Passing Data from Server to Client


Use props to pass data from Server to Client Components:


// app/[id]/page.tsx
import LikeButton from '@/app/ui/like-button'
import { getPost } from '@/lib/data'

export default async function Page({ params }) {
  const { id } = await params
  const post = await getPost(id)
  return <LikeButton likes={post.likes} />
}

Interleaving Server and Client Components


You can pass Server Components as children to Client Components:


// app/ui/modal.tsx
'use client'
export default function Modal({ children }) {
  return <div>{children}</div>
}

// app/page.tsx
import Modal from './ui/modal'
import Cart from './ui/cart'

export default function Page() {
  return (
    <Modal>
      <Cart />
    </Modal>
  )
}

Using Context in Client Components


React context is not supported in Server Components. Define it in a Client Component:


// app/theme-provider.tsx
'use client'
import { createContext } from 'react'
export const ThemeContext = createContext({})
export default function ThemeProvider({ children }) {
  return <ThemeContext.Provider value="dark">{children}</ThemeContext.Provider>
}

Third-party Component Integration


Wrap third-party components that rely on client-only features in a Client Component:


// app/gallery.tsx
'use client'
import { Carousel } from 'acme-carousel'

export default function Gallery() {
  return <Carousel />
}

Preventing Environment Variable Exposure


Only variables prefixed with NEXT_PUBLIC_ are exposed to the client. To protect server-only logic, use the server-only package:


// lib/data.ts
import 'server-only'

export async function getData() {
  const res = await fetch('https://...', {
    headers: { authorization: process.env.API_KEY }
  })
  return res.json()
}

Conclusion


Next.js enables powerful composition of Server and Client Components. By using 'use client' strategically, managing context, and protecting environment variables, you can build scalable, secure, and interactive applications with optimal performance.


Written & researched by Dr. Shahin Siami

Related Articles

Advanced Client-Side Routing and Performance Hooks in Next.js

Next.js provides a rich set of client-side hooks and caching utilities that empower developers to build dynamic, responsive, and secure applications. From reading route parameters to tracking navigation state and reporting performance metrics, this guide walks you through the most important tools available in the App Router.

Continue

Handling Authorization and Caching in Next.js: A Developer’s Guide

Next.js introduces powerful experimental features for access control and smart caching. This guide covers the unauthorized() function for custom 401 handling, unstable_cache for persistent memoization, updateTag for instant cache invalidation, and useLinkStatus for inline navigation feedback. Learn how to use these tools to build secure, performant, and responsive applications.

Continue

redirect and refresh in Next.js — Smart Redirects and Client Refreshing via Server Actions

The redirect function in Next.js allows you to navigate users to a new route, returning either a 307 or 303 HTTP response depending on context. It works in Server Components, Client Components, Route Handlers, and Server Actions. The refresh function is used exclusively within Server Actions to refresh the client router. This article explains how both functions work, with practical examples and key considerations.

Continue

NextRequest and NextResponse in Next.js — Managing Cookies, Headers, Redirects, and Rewrites

Next.js extends the native Web Request and Response APIs with NextRequest and NextResponse, offering powerful tools for managing cookies, headers, redirects, rewrites, and JSON responses. These utilities simplify server-side logic and improve control over routing, personalization, and security. This guide walks through their capabilities with practical examples and best practices.

Continue

headers, ImageResponse, notFound, and permanentRedirect in Next.js — Request Handling, Dynamic Images, Errors, and Redirects

Next.js offers powerful tools for handling HTTP requests and responses in Server Components. The headers function lets you read incoming request headers. ImageResponse allows you to generate dynamic images using JSX and CSS. The notFound function renders a custom 404 page, and permanentRedirect enables permanent redirection to another route. This article explains how to use each feature with practical examples.

Continue

A Complete Guide to Using metadata and generateMetadata in Next.js

In modern versions of Next.js, managing page metadata is more powerful and intuitive than ever. Metadata is automatically injected into the <head> of your pages and plays a vital role in SEO, social sharing, and user experience. This guide explains the two main ways to define metadata: using the static metadata object and the dynamic generateMetadata function.

Continue