Lesson 8/100

Tutorials Next.js Tutorial

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

BeginnerIntermediateAdvancedProfessional

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

CodeWhat 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

  1. Make a page async and await fetch to a public API
  2. View page source — content is in HTML
  3. Compare bundle size mindset: no useEffect needed for first paint
  4. Pass course data as props into a Client Component button
  5. Read the real-world section and name which part of LearnHub uses this topic.
  6. Run the example locally with npm run dev and confirm the same behavior.
  7. 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.

Interview prep for this lesson

Practice these questions aloud after reading—each links to a full structured answer.

Mid Detailed
What are common mistakes teams make with Components when using Next.js?
Short answer: Interviewers want a crisp definition, a practical example from your projects, and awareness of trade-offs—not textbook dumps. Explain a bit more How to structure your answer (60–90 seconds) Define Component…
Junior Detailed
Explain JavaScript in the context of Next.js.
Short answer: JavaScript runs single-threaded with an event loop. Closures capture lexical scope; promises/async handle I/O without blocking the UI thread. Real-world example (ShopNest) On the ShopNest storefront UI, thi…
Senior Detailed
How would you debug a production issue related to State in a Next.js application?
Short answer: Interviewers want a crisp definition, a practical example from your projects, and awareness of trade-offs—not textbook dumps. Explain a bit more How to structure your answer (60–90 seconds) Define State in…
Junior Detailed
Describe a real-world scenario where Performance mattered in a Next.js project.
Short answer: Interviewers want a crisp definition, a practical example from your projects, and awareness of trade-offs—not textbook dumps. Explain a bit more How to structure your answer (60–90 seconds) Define Performan…
Questions on this lesson 0

Sign in to ask a question or upvote helpful answers.

No questions yet — be the first to ask!

Next.js Tutorial
Course syllabus

Next.js Tutorial

Start here
Module 1: Next.js Foundations
Module 2: Layouts & Styling
Module 3: Data & Forms
Module 4: Auth & APIs
Module 5: SEO & Deploy
Module 6: Advanced Routing
Module 7: Auth & Database
Module 8: Quality & Security
Module 9: Cloud & Scale
Module 10: Portfolio Projects
Toolliyo Assistant
Ask about tutorials, ebooks, training, pricing, mentor services, and support. I use public site content only—not admin or internal tools.

care@toolliyo.com

Need callback? Share your details