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 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:
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.
For dynamic routes, the folder name uses square brackets. The dynamic segment is passed as a prop called params:
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.
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.
<html> + <body> + Navbar
Sidebar + children
Orders content
/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.
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.
error.tsx must include 'use client' at the top. Error boundaries use React lifecycle methods that only work in the browser.
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 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.
7. Link Component โ Client-Side Navigation
The Link component from next/link is the standard way to navigate between pages in Next.js. Like React Router's <Link>, it intercepts clicks and performs a client-side navigation โ no full page reload, no white-screen flash.
But Next.js Link goes a step further: it prefetches the target page in the background when the link enters the viewport. By the time the user clicks, the page content is already cached.
Link Props
| Prop | Type | Purpose |
|---|---|---|
href |
string | object | The target URL. Required. Can also be an object with pathname and query. |
replace |
boolean | Replaces the current history entry instead of pushing a new one. User can't press Back. |
prefetch |
boolean | null | Controls prefetching. null = default (prefetch in viewport). false = disable. true = always prefetch. |
scroll |
boolean | Scroll to top on navigation. Default true. Set false to preserve scroll. |
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.
===) 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.
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.
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:
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:
Metadata Inheritance
Metadata merges down the layout tree. The root layout sets site-wide defaults; pages override only what they need.
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:
| 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
layout.tsx in the Next.js App Router?loading.tsx do in Next.js?(shop) do to the URL?title and description in Next.js?You completed the Pages, Layouts & Navigation quiz.
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 โ