Server Components — Complete Guide
Server Components — Complete Guide: free step-by-step lesson with examples, common mistakes, and interview tips — part of Next.js Tutorial on Toolliyo Academy.
On this page
Next.js Tutorial (LearnHub) · Lesson 8 of 100
Server Components
Beginner → Intermediate → Advanced → Professional
Beginner · 1 — Foundations · ~12 min read · Module 1: Next.js Foundations
Introduction
This lesson is part of the beginner section. We explain Server Components slowly, with examples you can copy and run. If something is unclear, read it twice — that is how everyone learns. Server Components are the default in App Router. They run only on the server, can await fetch and database calls directly in the component, and send HTML to the browser without shipping unnecessary JavaScript. LearnHub course lists hit the database — students should not download huge JS bundles just to render static titles. Server Components keep secrets on the server too.
Server Components is setup knowledge. Without it, LearnHub will not run. Spend time here until npm run dev works without errors.
When will you use this?
You need this before writing any Next.js code — same as installing Node.js before opening a project.
- Every React/Next.js job expects you to run npx create-next-app and npm run dev on day one.
- Interviewers often ask you to explain the App Router folder structure and Server vs Client Components.
Real-world: HDFC-style banking dashboard
The Banking team building HDFC-style banking dashboard uses Server Components to load course data on the server so students get HTML fast. account holders never see the TypeScript files — they just get a fast, reliable accounts, transfers, and statement views.
Production-style code
// app/courses/page.tsx — Server Component (default)
async function getCourses() {
const res = await fetch('https://api.example.com/courses', {
next: { revalidate: 60 }
});
return res.json();
}
export default async function CoursesPage() {
const courses = await getCourses();
return (
<ul>
{courses.map((c: { id: string; title: string }) => (
<li key={c.id}>{c.title}</li>
))}
</ul>
);
}
What happens in production: In HDFC-style banking dashboard, getting Server Components right means account holders trust the accounts, transfers, and statement views every day.
Lesson example (start here)
Copy this smaller example first. Once it works, compare it with the real-world code above.
// app/courses/page.tsx — Server Component (no "use client")
async function getCourses() {
const res = await fetch('https://api.example.com/courses', {
next: { revalidate: 60 }
});
if (!res.ok) throw new Error('Failed to load courses');
return res.json() as Promise<{ id: string; title: string }[]>;
}
export default async function CoursesPage() {
const courses = await getCourses();
return (
<ul>
{courses.map((c) => (
<li key={c.id}>{c.title}</li>
))}
</ul>
);
}
Line-by-line walkthrough
| Code | What it means |
|---|---|
// app/courses/page.tsx — Server Component (no "use client") | Comment — notes for humans; the compiler ignores it. |
async function getCourses() { | Async function — can await fetch or database calls on the server. |
const res = await fetch('https://api.example.com/courses', { | Fetches data over HTTP — runs on the server in Server Components or in Server Actions. |
next: { revalidate: 60 } | Part of the Server Components example — read it together with the lines before and after. |
}); | Closes a block started by { above. |
if (!res.ok) throw new Error('Failed to load courses'); | Part of the Server Components example — read it together with the lines before and after. |
return res.json() as Promise<{ id: string; title: string }[]>; | Part of the Server Components example — read it together with the lines before and after. |
} | Closes a block started by { above. |
export default async function CoursesPage() { | Default export — the main page or component this file provides to Next.js. |
const courses = await getCourses(); | Part of the Server Components example — read it together with the lines before and after. |
return ( | Returns JSX — what the user sees in the browser. |
<ul> | Part of the Server Components example — read it together with the lines before and after. |
{courses.map((c) => ( | Part of the Server Components example — read it together with the lines before and after. |
<li key={c.id}>{c.title}</li> | Part of the Server Components example — read it together with the lines before and after. |
How it works (big picture)
- async function page can await fetch.
- Data loads before HTML is sent.
- API keys in fetch stay server-side.
- revalidate: 60 enables ISR-style refresh every minute.
Do this on your computer
- Make a page async and await fetch to a public API
- View page source — content is in HTML
- Compare bundle size mindset: no useEffect needed for first paint
- Pass course data as props into a Client Component button
- Read the real-world section and name which part of LearnHub uses this topic.
- Run the example locally with npm run dev and confirm the same behavior.
- Change one value in the example (route, text, or course id) and predict what will happen before you save.
Experiments — try changing this
- Change a string or route in the example and save — watch the browser update.
- Break the code on purpose (remove a bracket), read the error overlay, then fix it.
- Change the API URL or course id and see how the page data changes.
Remember
Default = Server Component. await data directly in the page. Pass serializable props to Client children.
Common questions
How do I know if a file is server or client?
If it has "use client" it is client; otherwise server (in app/).
How long should I spend on Server Components?
Until you can explain it in your own words and run the example without looking at the answer. Beginners often need 30–60 minutes per new concept; setup lessons may take one afternoon.
What if I get stuck on Server Components?
Re-read the line-by-line walkthrough, check the terminal and browser overlay for errors, and compare your code character-by-character with the example. Search the exact error text — someone else had it too.
Where is Server Components used in real jobs?
See the real-world section above — the same pattern appears in LMS, e-commerce, SaaS, and dashboards. Interviewers ask you to explain it using one concrete example.