ReactMedium

What are Server Components?

React Server Components (RSC) are components that run only on the server, either at build time or per request. They can be async, read databases, files and secrets directly, and their code never ships to the browser; React sends their rendered output to the client as a serialized payload. They became stable with React 19 and are used through frameworks such as Next.js and React Router.

Key properties

  • No client JavaScript for the component or its dependencies (markdown parsers, date libraries stay on the server).
  • Direct data access: await db.query(...) inside the component.
  • No interactivity: no useState, useEffect, event handlers or browser APIs.
  • Compose with Client Components: a Server Component can render "use client" components and pass serializable props or server-rendered children.
  • Streaming: async Server Components suspend, and <Suspense> boundaries stream results.

Related directives and APIs

  • "use client" marks the client boundary.
  • "use server" marks Server Functions callable from the client (for mutations).
  • cache() deduplicates work per request; cacheSignal() (React 19.2) aborts work when the cache lifetime ends.
  • React 19.3: Server Components can render Context providers from "use client" modules directly.

Benefits

Smaller bundles, faster loads, no client-server data waterfalls, and keeping sensitive logic on the server.

Security

Server Components and Server Functions are a server attack surface. Keep React and frameworks patched: critical RSC vulnerabilities were fixed in December 2025 (React 19.0.1, 19.1.2, 19.2.1 and framework releases).

Not the same as SSR

SSR renders any component to HTML; Client Components are still SSR'd and hydrated. RSC changes where components run and what ships.

Code example

tsx
1// app/products/page.tsx (Server Component in Next.js)
2import { db } from "@/lib/db"; // server-only module
3import { formatPrice } from "@/lib/fmt"; // runs on the server, not bundled for the browser
4import { AddToCart } from "./add-to-cart"; // Client Component
5
6export default async function Products() {
7 const products = await db.product.findMany({ take: 20 }); // direct database access
8
9 return (
10 <ul>
11 {products.map((p) => (
12 <li key={p.id}>
13 {p.name} - {formatPrice(p.price)}
14 <AddToCart productId={p.id} /> {/* only this ships JavaScript */}
15 </li>
16 ))}
17 </ul>
18 );
19}
20
21// app/products/add-to-cart.tsx
22// "use client";
23// import { useTransition } from "react";
24// import { addToCart } from "./actions"; // "use server" function
25// export function AddToCart({ productId }: { productId: string }) {
26// const [pending, start] = useTransition();
27// return <button disabled={pending} onClick={() => start(() => addToCart(productId))}>Add</button>;
28// }

Practice this question

Open it in the study app to track progress, take notes, listen to the answer and work through the rest of the React set.

Open in study app