🎓 A1Learner
🔍
0 of 50 lessons completed

Next.js Server Components Are the Default

A Server Component runs on the server. It sends HTML to the reader and adds no client code of its own.

A pure server page

The page reads the books and lists them. No line in it runs in the browser.

import { getBooks } from "@/lib/books";

export default function PurePage() {
  const books = getBooks();
  return (
    <main>
      <h1>My Bookshelf</h1>
      <p>Server-rendered book list</p>
      <ul>
        {books.map((book) => (
          <li key={book.id}>
            {book.title} by {book.author} ({book.year})
          </li>
        ))}
      </ul>
    </main>
  );
}

No button code on the pure page

The check counts the words Mark as read in the pure page. Zero means no button code shipped with it.

curl -s http://localhost:3111/pure | grep -c 'Mark as read'

Example

echo "pure: $(curl -s http://localhost:3111/pure | grep -o '<script[^>]*src=' | wc -l)"
echo "home: $(curl -s http://localhost:3111/ | grep -o '<script[^>]*src=' | wc -l)"

Key points

  • Server Component: a component that runs on the server, not in the browser
  • The server builds the HTML. The browser shows it as it arrives.
  • A pure server page ships no code for its own parts. The browser has nothing of yours to run.
  • The framework runtime scripts still load. They belong to Next.js, not to your page.
  • Server Components are the default. A component is a Server Component unless you say otherwise.

In this example, the check counts the script tags on two pages. The pure page has 6, and the home page has 7. The six are the shared framework scripts, and both pages load them. The seventh belongs to the home page alone. It is 746 bytes, and it holds the Mark as read button and the search box code. The pure page has no seventh script, so it ships no client code of its own.

Note

Reach for a Server Component first. Add client code only where the page needs it.

Try it Yourself

Edit the code, then press Run.

Running is simulated · TypeScript Playground

Exercise

What does a pure Server Component page ship for its own parts?