Chapter 20: Server-Side Rendering & Next.js Basics

Server-Side Rendering & Next.js Basics — this is the chapter that takes you from frontend-only React to full-stack, production-ready web applications!

Next.js is the most popular React framework in 2026 — used by companies like Netflix, TikTok, Vercel, OpenAI, and thousands of others. It gives you server-side rendering (SSR), static site generation (SSG), API routes, image optimization, automatic code splitting, and much more — all while keeping the developer experience amazing.

We’ll go very slowly and clearly, like I’m sitting next to you in Mumbai with our laptops open, explaining everything live with complete, copy-paste-ready examples.

1. Introduction to Next.js – What & Why?

Next.js is a React framework created by Vercel that adds powerful features on top of React:

  • Server-side rendering (SSR) → better SEO & faster first paint
  • Static site generation (SSG) → super fast & cheap hosting
  • Incremental Static Regeneration (ISR) → static + dynamic updates
  • App Router (new in v13+) → modern file-system-based routing
  • API Routes → build backend endpoints inside your app
  • Image Optimization, Font Optimization, Internationalization, Middleware
  • Deploy anywhere (Vercel is easiest, but also Netlify, Cloudflare, AWS…)

Why learn Next.js in 2026?

  • Almost every React job asks for Next.js
  • It’s the official React recommendation for production apps
  • You write normal React — but get superpowers

2. Create Your First Next.js Project (2026 Way)

Bash

→ Opens at http://localhost:3000

You’ll see a beautiful starter page — now let’s understand the two routing systems.

3. App Router vs Pages Router (Important!)

Next.js has two routing systems:

Feature App Router (recommended since v13 – 2026 standard) Pages Router (older, still supported)
Location app/ folder pages/ folder
Routing style File-system + folders (like Remix) File-system (every file = route)
Server Components Default (React Server Components) No
Data fetching async components + fetch with caching getStaticProps, getServerSideProps
Layouts Nested layouts with layout.tsx Manual or _app.tsx
Loading/Error UI Built-in loading.tsx, error.tsx Manual
Streaming & Suspense Native support No
Current recommendation Use this for all new projects Only for legacy code

Bottom line (2026): Always use App Router for new projects — it’s more powerful, faster, and future-proof.

4. Static vs Server Components (The Biggest Change in Next.js 13+)

In App Router, React introduced React Server Components (RSC).

Feature Server Components (default) Client Components (opt-in)
Where they run On the server (no JS sent to browser) On the client (like normal React)
Can use hooks? No (useState, useEffect, useRef…) Yes
Can fetch data? Yes (directly in component) Yes (but better to fetch on server)
Can access backend secrets? Yes (API keys, database…) No
SEO & performance Excellent (HTML sent from server) Good, but slower first paint
How to mark Default — no directive Add ‘use client’; at top of file

Rule of thumb:

  • Use Server Components by default (fetch data, render HTML)
  • Use Client Components only when you need interactivity (useState, onClick, useEffect)

5. Data Fetching in Next.js (App Router – Modern Way)

Next.js extends the native fetch API with smart caching and revalidation.

Example 1: Static Data Fetching (SSG – Build-time)

Create app/page.tsx

tsx

→ This page is statically generated at build time + revalidated every hour (Incremental Static Regeneration)

Example 2: Dynamic Data Fetching (SSR)

Create app/users/[id]/page.tsx (dynamic route)

tsx

→ This page is rendered on the server for every request (SSR)

Bonus: Loading UI (Streaming)

Create app/users/[id]/loading.tsx

tsx

→ Shows nice skeleton while data loads!

Summary – Chapter 20 Key Takeaways

  • Next.js = React framework for SSR, SSG, ISR, API routes
  • App Router (app/ folder) = modern way (use this!)
  • Server Components = default, run on server, no JS sent
  • Client Components = ‘use client’; when you need hooks/interactivity
  • Data fetching:
    • fetch with cache: ‘no-store’ → SSR
    • fetch with next: { revalidate: 3600 } → ISR
    • fetch with no options → static
  • loading.tsx, error.tsx → automatic loading & error UI

Mini Homework

  1. Create a Next.js app with:
    • Home page: list of users (static + ISR)
    • Dynamic /users/[id] page (SSR)
    • Nice loading skeleton
  2. Bonus: Add a simple API route /api/hello that returns JSON

You may also like...

Leave a Reply

Your email address will not be published. Required fields are marked *