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, nouseEffect - No event handlers (
onClick, etc.) - No browser APIs (
window,localStorage) - Can be
asyncโawaitdata 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
asyncat the top level
"use client" directive and move to the browser.
renders product data
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 |
"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.
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.
Compare: Old Way (Client Component) vs New Way (Server Component)
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.
| 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'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.
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.
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.
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
generateStaticParams
force-cache fetch
Stored on CDN
Instant response
cookies/headers
no-store fetch
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
Pattern B โ Static Category Pages
Pattern C โ Dynamic User Dashboard
โ 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
"use client" directive?cache: 'no-store' do on a fetch() call in Next.js?generateStaticParams used for?You completed the Data Fetching & Server Components quiz.
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 โ