Server Components in Next.js

Intermediate · 7 min

Why layouts and pages are Server Components by default in Next.js, what that lets you do, what it forbids, and how the RSC payload reaches the browser.

Before thisPages and layouts

Codevolution · 7 min

Server Components in Next.js

The player loads from youtube-nocookie.com when you press play. Nothing reaches YouTube before that.

Taught by CodevolutionReport a problem with this video
Not started
Set this lesson's status

What you'll learn

  • In the App Router, layouts and pages are Server Components by default: they run only on the server, so they can read a database or a secret directly and none of their code ships to the browser.
  • A Server Component is rendered into the RSC payload, a compact description of the UI that includes placeholders and file references for any Client Components inside it.
  • A Server Component can be `async` and `await` data, but it cannot use state, effects, event handlers or browser-only APIs, because it never runs in the browser.
  • On first load the browser gets HTML, the RSC payload and JavaScript for Client Components only; on later navigations it gets just the payload.

The situation

A traditional React page ships all its component code to the browser. The browser downloads it, runs it, and attaches event handlers to the HTML, which is hydration. That works, but every component, including the ones that only render static text from a database, costs download and execution time. And reading from a database means an extra API in between, because browser code cannot talk to a database.

Server Components change the split. A component that only needs to render data can run on the server, send its result, and never ship its own code. Only the parts that need interactivity travel as JavaScript. The React side of this idea is in server components vs client components; this lesson is how Next.js applies it.

The default

In the App Router, every layout and page is a Server Component unless you say otherwise. You do not add anything. A page can be async and await data in its body:

// app/products/page.tsx
import { db } from "@/lib/db";

export default async function Page() {
  const products = await db.product.findMany();
  return (
    <ul>
      {products.map((p) => (
        <li key={p.id}>{p.name}</li>
      ))}
    </ul>
  );
}

No useEffect, no loading state wired by hand, no API endpoint in the middle. The query runs on the server. The db module and any secret it uses never reach the browser. Fetching in depth comes in fetching data in Server Components.

Use Server Components when you need to fetch data close to its source, use secrets and tokens without exposing them, send less JavaScript, or improve how fast content first appears.

What they cannot do

They run once, on the server, and are not hydrated, so there is nothing in the browser to hold state or react to a click. Server Components cannot use useState, useReducer, useEffect, event handlers like onClick, or browser APIs like localStorage and window. If you need those, that piece is a Client Component, which is the next lesson. The usual design is mostly-server with small interactive leaves.

The RSC payload

How does server-rendered output reach the browser? On the server, Next.js splits rendering into chunks by route segment. Server Components render into a special format, the RSC payload: a compact serialized tree that contains

  • the rendered result of Server Components,
  • placeholders where Client Components go, plus references to their JavaScript files,
  • any props passed from Server to Client Components.

On a first visit, three things reach the browser. The HTML is a fast, non-interactive preview. The RSC payload lets React reconcile the server and client trees. The JavaScript hydrates just the Client Components. On later navigations through <Link>, only the payload is needed; the client renders the next route from it without the server HTML. That is why moving between pages in Next.js feels like a single-page app though the pages render on the server.

A subtle point: even a Client Component is rendered once on the server to produce that initial HTML, so “client” means “also runs in the browser”, not “never runs on the server”.

Props must cross a boundary

Data a Server Component hands to a Client Component travels through the payload, so it must be serializable: strings, numbers, plain objects, arrays, dates, promises. Functions and class instances do not serialize. The next lessons cover the boundary itself.

What the video covers

Codevolution’s chapters trace the evolution of React rendering from client-side rendering to SSR and Suspense for SSR, the problems of large bundles and unnecessary hydration, then introduce React Server Components, client components, server components, their benefits, combining the two, key takeaways and how this connects to Next.js. It is a concept video rather than a coding one, and it is the best match for this lesson’s “why.” Its companion, “Server and Client Components,” is linked under deeper and builds a project with both.

Where this fits

You know the default and what it costs. Next, the use client boundary is how you opt a piece into the browser.

Check yourself

  1. 01Why can a Server Component read an API key directly while a Client Component cannot?

    A Server Component's code runs only on the server and is never sent to the browser, so a secret it reads stays there. A Client Component's code is bundled and sent to every visitor, so anything in it is visible.

  2. 02What can't you do in a Server Component, and why?

    You cannot use `useState`, `useEffect`, event handlers such as `onClick`, or browser APIs such as `window`. A Server Component renders once on the server and is not hydrated, so there is no running component in the browser to hold state or respond to events.

  3. 03What is the RSC payload?

    A compact serialized description of the rendered Server Component tree. It holds the rendered output of Server Components, placeholders showing where Client Components go with references to their JavaScript, and the props passed across the boundary.

Go deeper

Notes

Private to you

Esc
↑↓ navigate↵ open? shortcuts

Keyboard shortcuts

Press Esc to close

Anywhere

⌘K
Search and commands
/
Search
?
This sheet
Esc
Close

Reading a lesson

J
Next lesson
K
Previous lesson
B
Bookmark