Skip to content
Tutorials Center
My learning

Topics

Lesson 15 of 72 ยท Server and Client Components

React Server Components

Learn what React Server Components are, how async components fetch data directly, why they ship zero client JavaScript, and what they can and cannot do in Next.js.

In a classic React app, every component is shipped to the browser as JavaScript, runs there, and fetches its data through an API. React Server Components (RSC) change that deal. A Server Component runs only on the server. Its code is never sent to the visitor; only the result of running it is. That one idea explains most of the App Router: why pages can read a database directly, why bundles are smaller, and why some familiar React features are suddenly off limits.

In the App Router, every component is a Server Component by default. You opt out only when you need interactivity, which is the subject of the next lesson. This lesson covers what a Server Component is, what it can do, what it cannot, and how the machinery works. The real Next.js cannot run on this site, so the framework code is read-only. The runnable examples are plain Node.js programs that model a tiny Server Component renderer, so you can watch components run, wait for data and turn into HTML.

What a Server Component is

Runs on the server At build time or per request, never in the browser.
Ships no code The browser gets output, not the component.
Can be async await your data right inside the component.

This is the default in the App Router.

1 / 3

A first Server Component

Here is a page that reads posts straight from a database. There is no API route, no useEffect, no loading state in the component, and no fetch call from the browser.

TSX
// app/blog/page.tsx (read-only)
import { db } from "@/lib/db";

export default async function BlogPage() {
  const posts = await db.post.findMany({ orderBy: { createdAt: "desc" } });

  return (
    <ul>
      {posts.map((post) => (
        <li key={post.id}>{post.title}</li>
      ))}
    </ul>
  );
}

Two features make this work. First, the component is declared async, so it can await. Plain React components in the browser cannot be async; Server Components can, because the server can simply wait before it continues. Second, db is imported and used on the server, so the database driver, its credentials and the query never travel to the browser. The visitor receives a list of <li> elements and nothing else about how it was produced.

Zero client JavaScript

The key benefit is the bundle. If BlogPage imports a large Markdown library to format post bodies, that library stays on the server. In a client-rendered app the same import adds tens of kilobytes to every visitor's download. With Server Components, you can use heavy libraries for formatting, syntax highlighting or data processing without paying for them in the browser.

The server turns the rendered tree into a special streamable format called the RSC payload, and also into HTML for the first paint. The browser displays the HTML immediately and uses the payload to know what the tree looks like. How a Request Flows Through Next.js and Inside the RSC Payload show this in more detail.

Trying it: a tiny Server Component renderer

The program below is a stand-in, not the real React. It models three ideas: components are functions that return a tree of elements, a component can be async, and the renderer waits for each one and produces plain HTML. The "database" is a local array with an artificial delay. Run it and read the log: you can see the order in which components ran on the "server", and the output at the end is all the browser would receive.

Node.js A mini async component renderer

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with node:readline or process.stdin), it gets the next line from this box.

Notice what is missing from the output: no function bodies, no getPosts, no database. Everything the visitor receives is markup. The real RSC payload also describes where interactive components go, as the next lesson explains.

What Server Components cannot do

Everything a Server Component does happens once, on the server, and the result is sent. That means anything that depends on the browser, or on the component re-running, is unavailable:

  • State and lifecycle: no useState, useReducer, useEffect or useLayoutEffect. There is no "later" for the component to react to.
  • Event handlers: onClick and onChange are functions, and functions cannot be sent to the browser as part of the payload.
  • Browser-only APIs: window, document, localStorage, navigator and friends do not exist in the server environment.
  • Context: createContext and useContext are for Client Components; a Server Component cannot read or provide a context value.
  • Libraries that use any of the above: a chart library that calls useEffect internally cannot be rendered directly in a Server Component.

If you try, Next.js stops with a clear error that names the offending hook and tells you to add "use client". The error is a friendly signal that the code needs to move to the browser, not a bug in your project.

The reverse limits apply too. A Server Component can import a Client Component and render it, but it can only pass it serializable props. Composing Server and Client Components covers those rules.

Trying it: spotting what needs the browser

The second program is a rough linter in the spirit of the error Next.js shows. It scans component source text for hooks, handlers and browser globals, and says whether the file can stay a Server Component. It is a pattern match for teaching, not a parser, so it is only a model of the real check.

Node.js Does this file need the browser?

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with node:readline or process.stdin), it gets the next line from this box.

ProductList can stay on the server, LikeButton needs state and handlers, and ThemeToggle reads window. The fix for the last two is exactly what the next lesson explains: move only that small interactive piece into a Client Component and keep the rest of the page on the server.

Server Components run once

A Server Component does not re-render in the browser when a user clicks something, because it does not exist in the browser. It runs when a page is built (for static pages) or when a request arrives (for dynamic pages). To get fresh data after a change, the browser asks the server for an updated payload, for example after a Server Action or a router.refresh(). The server re-runs the component and sends a new payload, which React merges into the page without a full reload.

Two practical consequences follow. Do not store per-visitor information in a module-level variable of a server file: the module is shared by all requests. And do not rely on state surviving between requests; keep that in a database, a cookie or the URL.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. In the App Router, what is the default for a component with no directive?

    Components are Server Components unless a file opts into the client with the use client directive.
  2. 2. Why can a Server Component be declared async?

    Server Components run to completion on the server, so they can await. The browser receives only the result.
  3. 3. Which of these can you do inside a Server Component?

    Database access and secrets are exactly what the server environment is for. State, event handlers and localStorage need the browser.
  4. 4. What happens to a large Markdown library imported only by a Server Component?

    Server Component code and its imports are never sent to the browser, so heavy server-only libraries cost visitors nothing.

Summary

  • Server Components run only on the server (at build time or per request); their code is never sent to the browser.
  • They can be async, read databases, files and secrets directly, and keep large libraries out of the client bundle.
  • They cannot use state, effects, event handlers, browser APIs or context; those need a Client Component.
  • The browser receives HTML for the first paint plus an RSC payload describing the tree.
  • A Server Component runs once per build or request; fresh data means the server runs it again and sends a new payload.

Next: Client Components and "use client"