1. How the App Router Works

Next.js 13+ introduced the App Router โ€” a file-system-based routing system where the name of a file determines its role, not just its folder location. Drop a page.tsx inside a folder and you have a route. Drop a layout.tsx next to it and every page in that folder shares that layout automatically.

This is a major shift from React Router, where you manually declared every route in code. In Next.js, the file tree is the route tree.

๐Ÿ›’
UrbanCart Example
UrbanCart's App Router file structure

UrbanCart has a home page, a products listing, individual product detail pages, a cart, and an account section. Here is how the App Router maps those URLs to files:

File โ†’ URL Mapping
app/ layout.tsx โ†’ wraps every page (root layout) page.tsx โ†’ / products/ page.tsx โ†’ /products [id]/ page.tsx โ†’ /products/42 cart/ page.tsx โ†’ /cart account/ layout.tsx โ†’ wraps /account/* only page.tsx โ†’ /account orders/ page.tsx โ†’ /account/orders
Key insight: Only the special filenames โ€” page.tsx, layout.tsx, loading.tsx, error.tsx, not-found.tsx โ€” are treated as route files. Any other file in the app/ directory (components, utilities, types) is ignored by the router.

2. page.tsx โ€” The Route's Content

page.tsx is the main content file for a route. Every URL that should render something needs a page.tsx in its folder. Without it, the folder is not a public route โ€” it's just organisation.

app/products/page.tsx Products listing page

For dynamic routes, the folder name uses square brackets. The dynamic segment is passed as a prop called params:

app/products/[id]/page.tsx Dynamic product detail route
File โ†’ URL mapping
app/page.tsx โ†’ / app/products/page.tsx โ†’ /products app/products/[id]/page.tsx โ†’ /products/1, /products/42, ... app/cart/page.tsx โ†’ /cart app/account/page.tsx โ†’ /account
โœ…

Server Component by default

Every page.tsx is a React Server Component unless you add 'use client' at the top. Server Components can fetch data directly, access databases, and never ship their code to the browser.

๐Ÿ“ฆ

Receives params & searchParams

params holds dynamic route segments like { id: "42" }. searchParams holds query string values like { sort: "price" } from /products?sort=price.

3. layout.tsx โ€” Shared UI Wrapper

layout.tsx wraps every page inside its folder and all sub-folders. When a user navigates between pages that share a layout, the layout does not re-render โ€” only the page content swaps. This keeps the navbar visible without a flicker and preserves scroll position in sidebars.

Root Layout (required)

The root layout at app/layout.tsx is mandatory. It must include the <html> and <body> tags โ€” Next.js does not add them automatically. It wraps every single page in your app.

app/layout.tsx Root layout โ€” wraps the entire app

Nested Layouts

You can add layout.tsx at any level of the folder tree. A nested layout only wraps pages within that segment. UrbanCart's /account section needs a sidebar that the main site doesn't โ€” a nested layout is the perfect solution.

app/account/layout.tsx Nested layout for the /account section
Layout Nesting for /account/orders
Root Layout
<html> + <body> + Navbar
โ†’
Account Layout
Sidebar + children
โ†’
page.tsx
Orders content
Important: Layouts do not re-render on navigation. When a user moves from /account/orders to /account/settings, the Root Layout and Account Layout stay mounted โ€” only the inner page.tsx swaps. This is what makes Next.js navigation feel instant.

4. loading.tsx โ€” Automatic Loading UI

When a page fetches data on the server, the user would otherwise see nothing until the data arrives. loading.tsx solves this: Next.js automatically wraps the page in a React Suspense boundary and shows the loading UI while the page renders.

You don't add any Suspense code yourself. Just create a loading.tsx next to your page.tsx and Next.js wires it up automatically.

app/products/loading.tsx Skeleton shown while products page loads
What Next.js does automatically
1. User navigates to /products 2. Next.js immediately shows loading.tsx content (skeleton) 3. products/page.tsx renders on the server (may fetch data) 4. When page is ready, Next.js swaps loading UI โ†’ page content 5. User sees instant feedback, never a blank white screen
Tip: loading.tsx is segment-scoped. A loading.tsx in app/products/ only activates for /products and its children โ€” it does not affect /cart or other routes.

5. error.tsx โ€” Automatic Error Boundary

Errors happen โ€” a product fetch might fail, a database query might time out. error.tsx lets you catch errors for a specific route segment without crashing the entire app. The rest of the layout stays intact; only the failing segment shows the error UI.

Must be a Client Component: error.tsx must include 'use client' at the top. Error boundaries use React lifecycle methods that only work in the browser.
app/products/error.tsx Error boundary for the /products segment โ€” MUST be 'use client'
๐Ÿ”

The reset function

Next.js passes a reset() function that attempts to re-render the segment. Useful for transient errors โ€” a retry often works without a full page reload.

๐Ÿงฑ

Segment-scoped

An error in /products only activates app/products/error.tsx. The Navbar, layout, and other pages keep working perfectly.

6. not-found.tsx โ€” Custom 404 Page

When a user visits a URL that doesn't exist โ€” like /products/9999 for a product that was deleted โ€” you call Next.js's notFound() function, and the nearest not-found.tsx in the folder tree is shown instead.

app/not-found.tsx Global 404 page for UrbanCart
Global vs. segment-scoped: app/not-found.tsx is the global 404 fallback. You can also add not-found.tsx inside any sub-folder to show a more specific message โ€” for example, app/products/[id]/not-found.tsx showing "Product not found" instead of the generic 404.

8. usePathname โ€” Highlight Active Nav Link

To highlight which page the user is currently on in a navbar, you need to know the current URL. The usePathname hook from next/navigation returns the current pathname as a string and updates whenever navigation happens.

Because it uses a React hook, any component that calls usePathname must be a Client Component โ€” add 'use client' at the top.

components/Navbar.tsx Active link highlighting with usePathname
How pathname matching works
Current URL pathname Products link active? / "/" No (exact match only for /) /products "/products" Yes (startsWith "/products") /products/42 "/products/42" Yes (startsWith "/products") /account/orders "/account/orders" No (different prefix)
Pattern: Use exact match (===) for the home route only. For all others, use startsWith(href) so the nav link stays active on sub-pages like /products/42 while browsing a product detail.

9. Route Groups โ€” Organise Without Affecting URLs

As your app grows, you might want to group related pages โ€” all marketing pages together, all shop pages together โ€” without those grouping folder names appearing in the URL. Route groups let you do exactly that.

Wrap a folder name in parentheses: (marketing), (shop), (auth). Next.js strips the parentheses from the URL completely.

๐Ÿ—‚๏ธ
UrbanCart Example
Separating marketing pages from shop pages
File structure with route groups
app/ (marketing)/ layout.tsx โ† marketing-specific layout page.tsx โ†’ / (home / landing page) about/ page.tsx โ†’ /about blog/ page.tsx โ†’ /blog (shop)/ layout.tsx โ† shop-specific layout (cart icon, etc.) products/ page.tsx โ†’ /products [id]/ page.tsx โ†’ /products/42 cart/ page.tsx โ†’ /cart checkout/ page.tsx โ†’ /checkout

Notice: (marketing) and (shop) do not appear in any URL. The routes are exactly the same as if those wrapper folders didn't exist โ€” but now you can give each group its own layout.tsx and keep code organised.

โœ…

What route groups do

Organise files into logical groups. Apply different layouts to different groups. Split large codebases cleanly. Team ownership (team A owns (shop)/, team B owns (marketing)/).

๐Ÿšซ

What route groups don't do

They do not affect URLs at all. (shop)/products/page.tsx and products/page.tsx both produce the URL /products. Route groups are 100% invisible to the browser.

Common use cases: Auth layout (login/signup need a centred card, no navbar), admin section (completely different design system), marketing pages vs. app pages, feature team separation in large codebases.

10. Metadata API โ€” SEO from Server Components

Search engines and social media previews depend on correct <title> and <meta> tags. Next.js has a built-in Metadata API that lets you export metadata from any Server Component โ€” page.tsx or layout.tsx โ€” and Next.js injects it into the HTML <head> automatically.

Static Metadata

For pages with fixed titles and descriptions, export a metadata object:

app/products/page.tsx Static metadata export

Dynamic Metadata with generateMetadata

For pages where the title depends on fetched data โ€” like a product detail page โ€” use the generateMetadata async function. It receives the same params as the page component:

app/products/[id]/page.tsx Dynamic metadata for product detail

Metadata Inheritance

Metadata merges down the layout tree. The root layout sets site-wide defaults; pages override only what they need.

Metadata merging example Root โ†’ page override
Resulting HTML <head> tags
Route / โ†’ <title>UrbanCart</title> Route /cart โ†’ <title>Your Cart | UrbanCart</title> Route /products/42 โ†’ <title>Wireless Headphones | UrbanCart</title>
Best practice: Always set the template in the root layout. This ensures every page automatically gets " | UrbanCart" appended without each page.tsx having to repeat it.

11. Putting It All Together โ€” UrbanCart File Structure

Here is what UrbanCart's complete App Router file tree looks like with all the conventions applied:

Complete UrbanCart App Router structure
app/ โ”‚ โ”œโ”€โ”€ layout.tsx โ† Root layout (html + body + Navbar) โ”œโ”€โ”€ page.tsx โ†’ / โ”œโ”€โ”€ not-found.tsx โ†’ Global 404 โ”œโ”€โ”€ globals.css โ”‚ โ”œโ”€โ”€ (marketing)/ โ† Route group โ€” no URL impact โ”‚ โ”œโ”€โ”€ layout.tsx โ† Marketing layout (no cart icon) โ”‚ โ”œโ”€โ”€ about/ โ”‚ โ”‚ โ””โ”€โ”€ page.tsx โ†’ /about โ”‚ โ””โ”€โ”€ blog/ โ”‚ โ””โ”€โ”€ page.tsx โ†’ /blog โ”‚ โ”œโ”€โ”€ (shop)/ โ† Route group โ€” no URL impact โ”‚ โ”œโ”€โ”€ layout.tsx โ† Shop layout (cart icon in header) โ”‚ โ”œโ”€โ”€ products/ โ”‚ โ”‚ โ”œโ”€โ”€ loading.tsx โ† Skeleton while products fetch โ”‚ โ”‚ โ”œโ”€โ”€ error.tsx โ† Error boundary (Client Component) โ”‚ โ”‚ โ”œโ”€โ”€ page.tsx โ†’ /products โ”‚ โ”‚ โ””โ”€โ”€ [id]/ โ”‚ โ”‚ โ”œโ”€โ”€ page.tsx โ†’ /products/42 โ”‚ โ”‚ โ””โ”€โ”€ not-found.tsx โ† "Product not found" 404 โ”‚ โ”œโ”€โ”€ cart/ โ”‚ โ”‚ โ””โ”€โ”€ page.tsx โ†’ /cart โ”‚ โ””โ”€โ”€ checkout/ โ”‚ โ””โ”€โ”€ page.tsx โ†’ /checkout โ”‚ โ””โ”€โ”€ account/ โ”œโ”€โ”€ layout.tsx โ† Account sidebar layout โ”œโ”€โ”€ page.tsx โ†’ /account โ”œโ”€โ”€ orders/ โ”‚ โ””โ”€โ”€ page.tsx โ†’ /account/orders โ””โ”€โ”€ settings/ โ””โ”€โ”€ page.tsx โ†’ /account/settings components/ Navbar.tsx โ† 'use client' โ€” uses usePathname
Summary table: Every special filename has a single purpose.
File Purpose Server or Client?
page.tsx Main content for a route segment Server (default)
layout.tsx Shared UI wrapping pages; persists across navigation Server (default)
loading.tsx Shown automatically (Suspense) while page loads Server (default)
error.tsx Error boundary for a segment; receives error + reset Client (required)
not-found.tsx Custom 404 when notFound() is called Server (default)
(group)/ Route group โ€” organises code, invisible in URLs N/A (folder)

Quiz

Question 1 of 5
What is the purpose of layout.tsx in the Next.js App Router?
Question 2 of 5
What does loading.tsx do in Next.js?
Question 3 of 5
Which component should you use for internal navigation in Next.js?
Question 4 of 5
What does a Route Group folder like (shop) do to the URL?
Question 5 of 5
Where do you define page metadata like title and description in Next.js?
0
/ 5
Great job!

You completed the Pages, Layouts & Navigation quiz.

Next Lesson

Data Fetching & Server Components โ†’
You've mastered App Router conventions. Next up: fetching data directly in Server Components, caching, and revalidation.
Africa & India's Best AI Academy
๐Ÿค– AI-first ๐ŸŽ“ Placement Assistance

You just wired up a full Next.js app with layouts, loading states, and SEO.
Now build a career that ships production apps.

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

Explore our AI-era courses โ†’