Next.jsMedium

What are the limitations of Server Components?

Server Components cannot hold state, run effects, handle browser events or use browser APIs, and they cannot read React context. Their props to Client Components must be serializable, and their output is only updated by a new server render.

Functional limits

  • No interactivity: no onClick, onChange or other handlers (except passing Server Functions to forms).
  • No state or lifecycle: useState, useReducer, useEffect, useRef for DOM.
  • No context: cannot call useContext.
  • No browser APIs: window, document, localStorage.
  • Not re-rendered on the client: updates require navigation, router.refresh(), or revalidation from a Server Function.

Composition limits

  • Client Components cannot import Server Components; they must receive them as props or children.
  • Props passed to Client Components must be serializable.
  • A Server Component inside a "use client" module's import graph becomes a Client Component.

Library limits

  • Libraries that rely on hooks or browser APIs must be wrapped in a Client Component.
  • Runtime CSS-in-JS libraries that inject styles at render time need a client registry, or do not work in Server Components at all.

Caching and request limits (Cache Components)

  • Inside "use cache", you cannot read cookies(), headers() or searchParams. Pass values as arguments or use "use cache: private".
  • Request-time work must be inside <Suspense> or cached, or the route is flagged as blocking.

Testing

Async Server Components are not fully supported by Jest or Vitest renderers; test them end-to-end with Playwright or test their data functions separately.

Code example

tsx
1// app/cart/page.tsx (Server Component)
2import { cookies } from "next/headers";
3import { QuantityPicker } from "./quantity-picker"; // "use client"
4import { getCart } from "@/lib/cart";
5
6export default async function Cart() {
7 const cartId = (await cookies()).get("cart")?.value;
8 const cart = await getCart(cartId);
9
10 // Not possible here:
11 // const [qty, setQty] = useState(1);
12 // <button onClick={() => setQty(qty + 1)} />
13 // window.localStorage.getItem("coupon");
14
15 return (
16 <ul>
17 {cart.items.map((item) => (
18 <li key={item.id}>
19 {item.name}
20 {/* interactivity is delegated to a Client Component */}
21 <QuantityPicker itemId={item.id} initial={item.qty} />
22 </li>
23 ))}
24 </ul>
25 );
26}

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 Next.js set.

Open in study app