Master Code
On The Go

Learn. Practice. Build.

Home › How-To › Next.js Routing

Next.js Guide

Next.js Routing — App Router

File-based routing is one of Next.js's best features. Folders become URLs, files become pages. Here's how to build every kind of route.

Quick answer: The App Router uses folders for routes. app/blog/page.tsx is /blog. app/blog/[slug]/page.tsx matches any slug. app/(marketing)/about/page.tsx is /about (the group name doesn't appear in the URL).

1

Static Routes

A folder with a page.tsx becomes a URL:

app/page.tsx                    →  /
app/about/page.tsx              →  /about
app/blog/page.tsx               →  /blog
app/contact/page.tsx            →  /contact
2

Nested Routes

app/blog/page.tsx                    →  /blog
app/blog/tech/page.tsx               →  /blog/tech
app/blog/tech/nextjs/page.tsx        →  /blog/tech/nextjs

Every folder adds a URL segment. Nest as deeply as you need.

3

Dynamic Routes

Square brackets create dynamic segments:

app/posts/[id]/page.tsx         →  /posts/1, /posts/abc, /posts/42

// app/posts/[id]/page.tsx
export default async function Post({ params }) {
    const { id } = await params;
    const post = await getPost(id);
    return <article>{post.title}</article>;
}

Multiple dynamic segments work too:

app/blog/[category]/[slug]/page.tsx
→ /blog/tech/nextjs-routing
→ /blog/news/announcing-v2
4

Catch-All Routes

app/docs/[...slug]/page.tsx

→ /docs/intro
→ /docs/api/auth
→ /docs/guides/nextjs/routing

// app/docs/[...slug]/page.tsx
export default async function Docs({ params }) {
    const { slug } = await params;
    // slug is an array: ['guides', 'nextjs', 'routing']
}

Use [[...slug]] (double brackets) to also match the base path /docs.

5

Layouts

A layout.tsx wraps every page in its folder:

// app/layout.tsx — root layout (applies to all pages)
export default function RootLayout({ children }) {
    return (
        <html>
            <body>
                <Navbar />
                {children}
                <Footer />
            </body>
        </html>
    );
}

// app/dashboard/layout.tsx — nested layout for /dashboard
export default function DashboardLayout({ children }) {
    return (
        <div className="flex">
            <Sidebar />
            <main>{children}</main>
        </div>
    );
}

Nested layouts stack — the root layout wraps everything, then the dashboard layout wraps its pages.

6

Route Groups

Parentheses organise files without adding URL segments:

app/(marketing)/about/page.tsx      →  /about
app/(marketing)/pricing/page.tsx    →  /pricing
app/(shop)/cart/page.tsx            →  /cart
app/(shop)/checkout/page.tsx        →  /checkout

Use groups to give different sections of your site different layouts — marketing pages with a big hero, shop pages with a cart sidebar.

7

Loading and Error States

Special files show loading and error UI automatically:

app/dashboard/loading.tsx    Loading spinner while page loads
app/dashboard/error.tsx      Error UI if page throws
app/not-found.tsx            Custom 404 page

Example loading.tsx:

export default function Loading() {
    return <div className="p-10">Loading...</div>;
}
8

Navigation with Link

import Link from 'next/link';

<Link href="/about">About</Link>
<Link href={`/posts/${post.id}`}>{post.title}</Link>

// Programmatic navigation
'use client';
import { useRouter } from 'next/navigation';

const router = useRouter();
router.push('/dashboard');

Always use <Link> instead of <a> for internal links — Next.js prefetches the destination.

📋 Routing Cheat Sheet

app/page.tsx                     /
app/about/page.tsx               /about
app/blog/[slug]/page.tsx         /blog/:slug
app/docs/[...slug]/page.tsx      /docs/a/b/c
app/(group)/page.tsx             / (group ignored)
app/layout.tsx                   wraps all pages
app/about/layout.tsx             wraps /about and below
app/loading.tsx                  loading UI
app/error.tsx                    error UI
app/not-found.tsx                404 page

❓ Frequently Asked Questions

How does routing work in Next.js?;

Folders define routes. A folder with page.tsx becomes a URL. Nested folders create nested URLs.

What is a dynamic route?

A route that matches any value, using [brackets]. Example: [id] matches /posts/1, /posts/2, etc.

What are route groups?

Folders in parentheses that organise code without affecting the URL. Useful for different layouts.

How do I create a 404 page?

Create app/not-found.tsx. Next.js shows it automatically for unmatched routes.

What about API routes?

Use app/api/route.ts files. They export GET, POST, PUT, DELETE handlers — like Express but built in.

🎯 What's Next?

← All How-To Guides