1. What is Next.js

Next.js is a React framework built by Vercel. Where plain React gives you a UI library, Next.js adds everything you need for a production web application: server-side rendering, static generation, file-based routing, built-in API routes, image optimisation, and more — all pre-configured and ready to go.

Think of React as the engine and Next.js as the full car: React renders components, but Next.js decides when and where those components render — on the server, at build time, or in the browser.

🛒
UrbanCart Example
Why UrbanCart needs Next.js

A customer searches Google for "wireless headphones under $50". For UrbanCart to appear in the results, Google's crawler must be able to read the product listings as HTML — not a blank page waiting for JavaScript to run. Next.js renders pages on the server and sends fully-formed HTML to both browsers and search engines, giving UrbanCart a real shot at organic traffic.

🖥️

Server-Side Rendering (SSR)

The server renders the React page to HTML on every request. The browser receives a fully-formed document — no white flash, instant first paint, fully crawlable by Google.

🏗️

Static Site Generation (SSG)

Pages are pre-built at deploy time and served as static files from a CDN. Ultra-fast — great for pages where the data doesn't change per request (e.g. a marketing homepage).

🗺️

File-Based Routing

Create a file in the app/ folder and you have a route. No react-router-dom configuration needed. The folder structure is the routing config.

🔌

Built-In API Routes

Create files in app/api/ to build backend endpoints inside the same project. Your frontend and lightweight backend live together — no separate Express server needed for simple cases.

2. Why Next.js — vs CRA / Vite

You already know React. You may have used Create React App (CRA) or Vite to bootstrap projects. So why switch to Next.js? The short answer: CRA and Vite produce client-side only apps. The browser downloads JavaScript and then builds the page. This creates real problems for e-commerce.

CRA / Vite vs Next.js — What Google sees
CRA / Vite
Google gets: <div id="root"></div>
vs
Next.js (SSR)
Google gets: full HTML with product data
Feature CRA / Vite Next.js
SEO Poor — blank HTML until JS loads Excellent — server-rendered HTML
Initial load time Slower — JS must run first Faster — HTML arrives pre-rendered
Routing Manual — install react-router-dom Built-in — folder structure = routes
Image optimisation Manual setup required Built-in <Image> component
API endpoints Separate server project needed Built-in API routes in app/api/
TypeScript Manual configuration First-class support out of the box
Industry standard: Flipkart, Swiggy, Shopify, and thousands of e-commerce companies use Next.js or similar SSR frameworks specifically for SEO and performance. If you're building something customer-facing that needs to rank on Google, Next.js is the right tool.

3. App Router vs Pages Router

Next.js has two routing systems. Understanding the difference saves confusion when you search docs or read older code.

✅

App Router (use this)

Introduced in Next.js 13, default since Next.js 14. Lives in the app/ directory. Uses React Server Components by default, supports streaming, nested layouts, and loading.tsx / error.tsx conventions. This is what we use in this track.

📚

Pages Router (legacy)

The original Next.js routing system. Lives in the pages/ directory. Still works and is fully supported, but new projects should use the App Router. You'll encounter Pages Router in older codebases and tutorials — the concepts are similar but the file conventions differ.

When you see old tutorials: If a Next.js tutorial uses pages/index.js, getServerSideProps, or getStaticProps, it's using the Pages Router. Our track uses the App Router: app/page.tsx, Server Components, and async components with direct fetch() calls.
App Router vs Pages Router — file comparison
App Router (Next.js 13+) Pages Router (legacy) ───────────────────────────────────────────────────── app/page.tsx ←→ pages/index.js app/products/page.tsx ←→ pages/products.js app/layout.tsx ←→ pages/_app.js app/api/route.ts ←→ pages/api/handler.js async function Page() ←→ getServerSideProps() loading.tsx ←→ (custom implementation)

4. Project Structure

When you run create-next-app, it scaffolds a standard project structure. Understanding each part tells you where to put code and why.

UrbanCart Next.js — full project file tree
urbancart-next/ ├── app/ ← All pages & API routes live here │ ├── layout.tsx ← Root layout (wraps every page) │ ├── page.tsx ← Homepage → / │ ├── globals.css ← Global styles │ ├── products/ │ │ └── page.tsx ← Products page → /products │ ├── product/ │ │ └── [id]/ │ │ └── page.tsx ← Dynamic product → /product/:id │ ├── cart/ │ │ └── page.tsx ← Cart → /cart │ ├── checkout/ │ │ └── page.tsx ← Checkout → /checkout │ └── api/ │ └── products/ │ └── route.ts ← API endpoint → GET /api/products │ ├── components/ ← Reusable UI components │ ├── Navbar.tsx │ ├── ProductCard.tsx │ └── Footer.tsx │ ├── public/ ← Static assets (images, fonts, etc.) │ ├── logo.svg │ └── hero-banner.jpg │ ├── .env.local ← Secret keys (never commit this) ├── next.config.js ← Next.js configuration ├── tailwind.config.ts ← Tailwind CSS config (if used) ├── tsconfig.json ← TypeScript configuration └── package.json ← Dependencies & npm scripts
📁

app/

The heart of the App Router. Every page.tsx here becomes a URL. Nested folders create nested routes.

🧩

components/

Reusable React components shared across pages. Not a Next.js convention — just good organisation.

🖼️

public/

Static files served directly. A file at public/logo.svg is accessible at /logo.svg.

🔑

.env.local

Environment variables — API keys, database URLs. Never commit this file. Access with process.env.VAR_NAME.

⚙️

next.config.js

Next.js configuration: image domains, redirects, environment variables, custom headers, and more.

📦

package.json

Dependencies and scripts. npm run dev starts the dev server. npm run build creates the production build.

5. File-Based Routing

This is one of Next.js's most powerful ideas. You don't write route configuration — you just create files. The folder structure inside app/ directly maps to URLs in your app.

The rule is simple: a file called page.tsx inside a folder creates a route. The URL is determined by the folder path.

File Path URL Description
app/page.tsx / UrbanCart homepage
app/products/page.tsx /products Product listing page
app/cart/page.tsx /cart Shopping cart
app/checkout/page.tsx /checkout Checkout flow
app/product/[id]/page.tsx /product/:id Dynamic product detail page
app/api/products/route.ts /api/products API endpoint (returns JSON)

Dynamic Routes — [id] folders

Square brackets in a folder name create a dynamic segment. The folder app/product/[id]/ matches /product/1, /product/42, /product/wireless-headphones — any value after /product/.

app/product/[id]/page.tsx Dynamic Route — Product Detail
File → URL Mapping
app/page.tsx
→ /
·
app/products/page.tsx
→ /products
·
app/product/[id]/page.tsx
→ /product/:id
Special files in App Router: Inside any route folder, Next.js recognises specific filenames with special meaning: page.tsx (the page UI), layout.tsx (shared wrapper), loading.tsx (loading state), error.tsx (error boundary), and not-found.tsx (404 page). Only page.tsx is required.

6. Core Files — Code Examples

Let's look at the key files that every Next.js App Router project starts with. These are read-only examples — you'll write your own versions in the exercises.

app/layout.tsx — Root Layout

The root layout is a special file that wraps every page in your app. It's the ideal place for your <html> and <body> tags, global styles, shared navigation, and metadata. It renders once and persists across page navigations.

app/layout.tsx Root Layout — wraps every page

app/page.tsx — Homepage

The file app/page.tsx maps to the root URL /. It's a React Server Component by default, meaning it runs on the server and can directly await data without useEffect.

app/page.tsx Homepage — maps to /

app/products/page.tsx — Products Page

Because this component is async, it can fetch data directly on the server and pass it to the rendered HTML — no loading spinners, no useEffect.

app/products/page.tsx Products page — maps to /products
Server Components are the default: In the App Router, every component is a React Server Component unless you add "use client" at the top. Server Components run on the server, can fetch data directly, and send HTML to the browser. "use client" is only needed when you require browser APIs, useState, useEffect, or event handlers.

7. Running the Dev Server

Getting a Next.js project running takes three commands. The create-next-app CLI handles all the scaffolding — TypeScript, ESLint, Tailwind (optional), and the App Router are all set up automatically.

Step 1 — Create the project

terminal Bootstrap a new Next.js app
create-next-app interactive prompts
✔ What is your project named? … urbancart-next ✔ Would you like to use TypeScript? … Yes ✔ Would you like to use ESLint? … Yes ✔ Would you like to use Tailwind CSS? … Yes ✔ Would you like your code inside a `src/` directory? … No ✔ Would you like to use App Router? (recommended) … Yes ✔ Would you like to use Turbopack for next dev? … Yes ✔ Would you like to customize the import alias (@/*)? … No Creating a new Next.js app in /Users/you/urbancart-next... Installing dependencies: - react - react-dom - next Installing devDependencies: - typescript - @types/node - @types/react - eslint - eslint-config-next - tailwindcss ✓ Success! Created urbancart-next

Step 2 — Start the dev server

terminal Navigate and start
npm run dev output
▲ Next.js 14.2.0 (Turbopack) - Local: http://localhost:3000 - Network: http://192.168.1.42:3000 - Environments: .env.local ✓ Starting... ✓ Ready in 847ms Route (app) Size First Load JS ┌ ○ / 5.3 kB 92.4 kB ├ ○ /products 3.1 kB 89.8 kB ├ ○ /cart 2.4 kB 89.1 kB ├ ○ /checkout 4.2 kB 91.3 kB └ ● /product/[id] 3.7 kB 90.4 kB ○ (Static) prerendered as static content ● (Dynamic) server-rendered on demand
What the output tells you: Next.js lists every route it found and whether it's ○ Static (generated at build time, super fast) or ● Dynamic (rendered per request, needed for personalised data). UrbanCart's product detail pages are dynamic because each request needs the specific product data.

Step 3 — Open in browser

Visit http://localhost:3000 in your browser. Hot Module Replacement (HMR) means any file you save will instantly update the browser without a full reload. Changes to Server Components re-render on the server and stream the update to your browser.

Available at
http://localhost:3000 → app/page.tsx (Homepage) http://localhost:3000/products → app/products/page.tsx http://localhost:3000/cart → app/cart/page.tsx http://localhost:3000/product/1 → app/product/[id]/page.tsx

Quiz

Question 1 of 5
What is the main advantage of Next.js over Create React App for an e-commerce site like UrbanCart?
Question 2 of 5
In the App Router, what specific file do you create to make a URL accessible as a page?
Question 3 of 5
What does the layout.tsx file do in the Next.js App Router?
Question 4 of 5
How do you create a dynamic route like /product/:id in Next.js App Router?
Question 5 of 5
What command starts the Next.js development server?
0
/ 5
Great job!

You completed the Introduction to Next.js quiz.

Next Lesson

Pages, Layouts & Navigation →
You understand the fundamentals! Next up: building real pages and navigation in Next.js.
Africa & India's Best AI Academy
🤖 AI-first 🎓 Placement Assistance

You just learned Next.js — the framework powering
the world's biggest e-commerce sites.

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

Explore our AI-era courses →