1. Server Components vs Client Components

The biggest shift in Next.js 13+ App Router is that every component is a Server Component by default. Server Components run only on the server โ€” they never ship JavaScript to the browser. To opt into browser interactivity, you add "use client" at the top of the file.

๐Ÿ–ฅ๏ธ

Server Component Default

Runs on the server. Can directly fetch data, read databases, and use secrets. Zero JS sent to the browser.

  • No useState, no useEffect
  • No event handlers (onClick, etc.)
  • No browser APIs (window, localStorage)
  • Can be async โ€” await data directly
๐Ÿ“ฑ

Client Component "use client"

Runs in the browser (and server for initial HTML). Needed for all interactivity and browser APIs.

  • Can use useState, useEffect
  • Can attach event handlers
  • Can access window, localStorage
  • Cannot be async at the top level
The mental model: Think of your Next.js app as a tree of components. The root is on the server. As you go deeper, most nodes stay on the server. Only the leaves that need interactivity get the "use client" directive and move to the browser.
Component Tree โ€” UrbanCart
page.tsx (Server) โ€” fetches product list
โ†“
ProductCard (Server)
renders product data
AddToCartBtn (Client)
needs onClick + useState

Server Components can import Client Components โ€” but not the reverse.

2. When to Use Each

The rule is simple: use Server Components for everything that can be on the server. Only reach for "use client" when you genuinely need browser capabilities.

Scenario Server Component Client Component
Fetch products from UrbanCart API โœ“ Preferred Possible (useEffect)
Read from a database directly โœ“ Only here โœ— Not possible
Use API keys / secrets โœ“ Safe (not in bundle) โœ— Exposed to browser
Add to cart button (onClick) โœ— No event handlers โœ“ Required
Search input (onChange + useState) โœ— No state โœ“ Required
Carousel with useEffect animation โœ— No useEffect โœ“ Required
Static product description text โœ“ Preferred Unnecessary JS overhead
Access localStorage (cart persistence) โœ— No browser APIs โœ“ Required
Performance tip: Every "use client" boundary adds JavaScript to your bundle. Keep Client Components as small and deep in the tree as possible. Fetch data in Server Components and pass it down as props to small Client Components that only handle interactivity.

3. fetch() in Server Components

In traditional React, you fetch data with useEffect โ€” which runs after rendering, causing a flash of empty content. In Next.js Server Components, you await fetch() directly inside the component. No useEffect, no loading state needed.

๐Ÿ›’
UrbanCart Example
Server Component: Products page

The UrbanCart products page fetches the product list on the server, renders the HTML, and sends a complete page to the browser โ€” no loading spinner, no layout shift.

app/products/page.tsx Server Component โ€” fetches data directly
What Next.js does with this
1. Build/request time: Next.js calls getProducts() on the server 2. Awaits the fetch โ€” no loading state needed in the UI 3. Renders the full HTML with all 24 products 4. Sends complete HTML to the browser 5. Browser shows fully-populated page instantly 6. Zero JavaScript for the product list itself

Compare: Old Way (Client Component) vs New Way (Server Component)

OldProductsPage.tsx Client Component โ€” the old useEffect pattern (avoid this)
Server Component wins: The server component sends fully-rendered HTML on the first request. No JavaScript for the data-fetching logic, no loading flicker, and secrets stay on the server.

4. Caching โ€” Default, no-store, and revalidate

Next.js extends the native fetch() API with caching options. The same URL can behave differently depending on how you configure the cache. This is how Next.js implements three rendering strategies with a single API.

app/lib/fetchers.ts Three caching strategies side by side
Strategy Cache Option When Fetched UrbanCart Use Case
Static (SSG) force-cache (default) Once at build time Category names, footer links
ISR next: {"{"}revalidate: 60{"}"} Build + every 60s Product listings, prices
Dynamic (SSR) no-store Every request Cart count, user orders
๐Ÿ’ก
UrbanCart Example
Mixing strategies on one page

UrbanCart's homepage fetches featured products with ISR (revalidate every 5 minutes โ€” prices might change), static category links (built once โ€” categories rarely change), and passes the user ID to a Client Component which fetches live cart count from the browser. Each piece of data uses the right strategy.

5. generateStaticParams โ€” Pre-building Product Pages

UrbanCart has thousands of product pages at /product/[id]. You want them fast โ€” that means pre-building them as static HTML at deploy time, not generating them on-demand per request. generateStaticParams tells Next.js which URLs to pre-build.

app/product/[id]/page.tsx generateStaticParams โ€” pre-build all product pages
How it works: generateStaticParams runs once at build time. Next.js calls your API, gets all product IDs, and builds a static HTML file for each one. When a user visits /product/42, they get a pre-built file from the CDN โ€” blazing fast, no server computation needed at request time.
Next.js build output โ€” static product pages
Route (app) Size First Load JS โ”œ โ—‹ / 4.1 kB 89.2 kB โ”œ โ—‹ /products 3.2 kB 88.3 kB โ”œ โ— /product/[id] 2.8 kB 87.9 kB โ”‚ โ”œ /product/1 โ”‚ โ”œ /product/2 โ”‚ โ”” /product/3 โ”” โ—‹ /categories 1.9 kB 87.0 kB โ—‹ (Static) pre-rendered as static content โ— (SSG) pre-rendered as static HTML (uses generateStaticParams) ฮป (Dynamic) server-rendered on demand

6. Dynamic vs Static Rendering

Next.js automatically decides whether a route is static or dynamic. You don't always configure this manually โ€” Next.js infers it from your code. Understanding the rules helps you write faster pages.

Rendering decision rules When Next.js chooses static vs dynamic
โšก

Static Rendering Build time

HTML generated once and cached on a CDN. Fastest possible response โ€” no server computation per request. Best for content that doesn't change per user.

  • Product description pages
  • Category pages
  • Blog posts, marketing pages
๐Ÿ”„

Dynamic Rendering Per request

HTML generated on every request. Slower but always up-to-date and user-specific. Necessary for personalised or real-time data.

  • User dashboard (/account)
  • Cart page (user-specific)
  • Order history
  • Search results page
Rendering Flow
Build time
generateStaticParams
force-cache fetch
โ†’
Static HTML
Stored on CDN
Instant response
vs
Request time
cookies/headers
no-store fetch
โ†’
Dynamic HTML
Per-request render
Always fresh

7. UrbanCart โ€” Full Patterns in Practice

Let's bring it all together. Here are three complete UrbanCart components showing how Server Components, Client Components, and different caching strategies work together in a real application.

Pattern A โ€” Client Component: Add to Cart Button

app/components/AddToCartButton.tsx Client Component โ€” needs useState and onClick

Pattern B โ€” Static Category Pages

app/categories/[slug]/page.tsx Static category pages โ€” built at deploy time

Pattern C โ€” Dynamic User Dashboard

app/account/orders/page.tsx Dynamic page โ€” user-specific, rendered per request
Build output โ€” UrbanCart full app
Route (app) Size First Load JS โ”œ โ—‹ / 4.1 kB 89.2 kB โ”œ โ—‹ /products 3.2 kB 88.3 kB โ”œ โ— /product/[id] 2.8 kB 87.9 kB โ”‚ โ”œ /product/1 โ”‚ โ”œ /product/2 โ”‚ โ”” /product/3 โ”œ โ— /categories/[slug] 2.1 kB 87.2 kB โ”‚ โ”œ /categories/electronics โ”‚ โ”œ /categories/clothing โ”‚ โ”” /categories/home โ”” ฮป /account/orders 1.4 kB 86.5 kB โ—‹ (Static) pre-rendered as static content โ— (SSG) pre-rendered as static HTML (uses generateStaticParams) ฮป (Dynamic) server-rendered on demand โœ“ Compiled successfully โœ“ 847 modules transformed โœ“ Generating static pages (6/6) ...
Reading the build output: โ—‹ means the page is fully static (from CDN). โ— means pre-built with generateStaticParams โ€” also static, but from dynamic routes. ฮป means rendered on the server per request. Aim for as many โ—‹ and โ— as possible.

Quiz

Question 1 of 5
What is a Server Component in Next.js?
Question 2 of 5
When do you need the "use client" directive?
Question 3 of 5
What does cache: 'no-store' do on a fetch() call in Next.js?
Question 4 of 5
What is generateStaticParams used for?
Question 5 of 5
What is ISR (Incremental Static Regeneration)?
0
/ 5
Great job!

You completed the Data Fetching & Server Components quiz.

Next Lesson

API Routes & Server Actions โ†’
You've mastered data fetching. Next: build backend endpoints right inside your Next.js app.
Africa & India's Best AI Academy
๐Ÿค– AI-first ๐ŸŽ“ Placement Assistance

You just mastered Next.js Server Components.
Now build a full-stack career that scales.

Join the Best AI Programs โ€” three 5-month courses, fully online, with placement assistance. Week 1 is free.

Explore our AI-era courses โ†’