In the React courses, every component ran in the browser. To show data
from a server, a component rendered once with nothing, ran an effect, fetched,
stored the answer in state, and rendered again.
In the App Router, pages and layouts are Server Components by default.
They run on the server, where the data is, and the browser receives only what
they rendered. Their code is never sent to the browser.
A page that loads dataNext.js · 4 filesimport "./globals.css";
export default function RootLayout({ children }) {
return (
<html lang="en">
<body>
<header>Study Hub</header>
{children}
</body>
</html>
);
}
import { getTopics } from "../lib/topics.js";
export default async function HomePage() {
const topics = await getTopics();
return (
<main>
<h1>Topics</h1>
<ul>
{topics.map((topic) => (
<li key={topic.id}>
{topic.title} <span className="muted">· Day {topic.day}</span>
</li>
))}
</ul>
</main>
);
}
body {
font: 16px/1.5 system-ui, sans-serif;
margin: 0;
color: #1c2430;
}
header,
main,
footer {
max-width: 36rem;
margin: 0 auto;
padding: 0.75rem 1rem;
}
header {
border-bottom: 1px solid #d6dde6;
}
nav {
display: flex;
gap: 1rem;
}
a {
color: #06617f;
}
h1 {
margin: 0.5rem 0;
}
.muted {
color: #5b6876;
}
.card {
border: 1px solid #d6dde6;
border-radius: 8px;
padding: 0.75rem 1rem;
margin-bottom: 0.75rem;
}
// Pretend database: in a real app this would query a database or call an API.
const topics = [
{ id: 1, title: "Pages and routes", day: 1, minutes: 20 },
{ id: 2, title: "Layouts and links", day: 1, minutes: 20 },
{ id: 3, title: "Server components", day: 2, minutes: 20 },
{ id: 4, title: "Client components", day: 2, minutes: 20 },
{ id: 5, title: "Dynamic routes", day: 3, minutes: 25 },
];
export async function getTopics() {
console.log("Reading topics from the database");
await new Promise((resolve) => setTimeout(resolve, 200));
return topics;
}
Runs with real Next.js on your computer: unzip, then npm install and npm run dev.
A server component can be an async function and simply await its data. No
effect, no loading state to manage, no second render: the page is built with
the data already in it.
getTopics() is in lib/topics.js. Here it's a pretend database; in a real
app it would query one. Because it only ever runs on the server, its code, and
server-only credentials it uses need not be shipped to the browser. The data
you return or pass as props does reach the reader, so don't include secrets
in that result. The lab's pretend database has no real credentials:
JavaScript
export async function getTopics() { console.log("Reading topics from the database"); await new Promise((resolve) => setTimeout(resolve, 200)); return topics;}
Predict
Look at the console. Where did Reading topics from the database come
from, and would it appear in the browser's developer tools on a real site?
Check your prediction
It's marked [server]: the server ran it. On a real site, it appears in the
terminal running Next.js. Development tooling can forward server logs to the
browser's console too. The important fact is where the function executes,
not which devtools window displays its log.
What a server component can't do
Server Components run on the server and are done once their result is sent.
They don't keep state between clicks or handle browser events. Try it: add
import { useState } from "react"; and a useState call to the page.
What happens
The page doesn't compile:
You're importing a module that depends on useState into a React Server
Component module. This API is only available in Client Components. To fix, mark
the file (or its parent) with the "use client" directive.
Real Next.js shows the same message. The next lesson is about that directive.
Remove the useState lines.
Exercise
Total time
Under the list, show 105 minutes in total: the sum of every topic's
minutes.
Your working spaceNext.js · 4 filesimport "./globals.css";
export default function RootLayout({ children }) {
return (
<html lang="en">
<body>
<header>Study Hub</header>
{children}
</body>
</html>
);
}
import { getTopics } from "../lib/topics.js";
export default async function HomePage() {
const topics = await getTopics();
return (
<main>
<h1>Topics</h1>
<ul>
{topics.map((topic) => (
<li key={topic.id}>
{topic.title} <span className="muted">· Day {topic.day}</span>
</li>
))}
</ul>
</main>
);
}
body {
font: 16px/1.5 system-ui, sans-serif;
margin: 0;
color: #1c2430;
}
header,
main,
footer {
max-width: 36rem;
margin: 0 auto;
padding: 0.75rem 1rem;
}
header {
border-bottom: 1px solid #d6dde6;
}
nav {
display: flex;
gap: 1rem;
}
a {
color: #06617f;
}
h1 {
margin: 0.5rem 0;
}
.muted {
color: #5b6876;
}
.card {
border: 1px solid #d6dde6;
border-radius: 8px;
padding: 0.75rem 1rem;
margin-bottom: 0.75rem;
}
// Pretend database: in a real app this would query a database or call an API.
const topics = [
{ id: 1, title: "Pages and routes", day: 1, minutes: 20 },
{ id: 2, title: "Layouts and links", day: 1, minutes: 20 },
{ id: 3, title: "Server components", day: 2, minutes: 20 },
{ id: 4, title: "Client components", day: 2, minutes: 20 },
{ id: 5, title: "Dynamic routes", day: 3, minutes: 25 },
];
export async function getTopics() {
console.log("Reading topics from the database");
await new Promise((resolve) => setTimeout(resolve, 200));
return topics;
}
Runs with real Next.js on your computer: unzip, then npm install and npm run dev.
Hint 1: the idea
The total follows from data already loaded on the server. It needs no client state or second request.
Hint 2: a first step
The data is already in topics. Work out the sum with reduce before the
return, as you would in any component.