File-based Routing

Loading concept...

๐Ÿ—บ๏ธ Next.js File-based Routing: Your Appโ€™s GPS System

The Big Idea: Folders = Roads in Your App

Imagine your favorite pizza place. When you walk in, there are clear signs:

  • โ€œOrder Hereโ€ โ†’ takes you to the counter
  • โ€œPick Upโ€ โ†’ takes you to get your food
  • โ€œRestroomsโ€ โ†’ you know where that goes!

Next.js routing works exactly like this. Your folders are the signs, and your files are the destinations. No complicated setup neededโ€”just create a folder, and boom, you have a new page!


๐Ÿ  Pages and Routes: Building Your First Rooms

Whatโ€™s a Route?

A route is just a path in your websiteโ€™s address bar. Like:

  • mysite.com/ โ†’ Home
  • mysite.com/about โ†’ About page
  • mysite.com/contact โ†’ Contact page

How Next.js Makes This Easy

In Next.js (App Router), you create routes by making folders inside the app folder:

app/
โ”œโ”€โ”€ page.tsx      โ†’ mysite.com/
โ”œโ”€โ”€ about/
โ”‚   โ””โ”€โ”€ page.tsx  โ†’ mysite.com/about
โ””โ”€โ”€ contact/
    โ””โ”€โ”€ page.tsx  โ†’ mysite.com/contact

Thatโ€™s it! Folder name = URL path. Simple as pizza!

Real Example

Want a blog section? Just create:

app/
โ””โ”€โ”€ blog/
    โ””โ”€โ”€ page.tsx

Now mysite.com/blog works automatically. Magic? Nopeโ€”just smart design!


๐Ÿ“„ Page Component: The Star of Every Route

What Makes a Page Special?

Every route needs one special file called page.tsx (or page.js). This file is like the welcome mat for that routeโ€”itโ€™s what visitors see first.

Your First Page Component

// app/about/page.tsx

export default function AboutPage() {
  return (
    <div>
      <h1>About Us</h1>
      <p>We make awesome apps!</p>
    </div>
  );
}

The Secret Rules

  1. Must be named page.tsx - Other names wonโ€™t work
  2. Must export a default function - This is your component
  3. One per folder - Each route gets one page

Think of it like a house: each room (folder) can only have one front door (page.tsx).


๐ŸŽญ Dynamic Routes: One Template, Many Pages

The Problem

Imagine you have 1000 products. Do you create 1000 folders?

  • app/product-1/page.tsx
  • app/product-2/page.tsx
  • โ€ฆ (yikes!)

NO WAY! Thatโ€™s crazy talk!

The Solution: Square Brackets

Use [squareBrackets] to create a flexible route:

app/
โ””โ”€โ”€ products/
    โ””โ”€โ”€ [id]/
        โ””โ”€โ”€ page.tsx

Now ONE file handles:

  • mysite.com/products/1
  • mysite.com/products/999
  • mysite.com/products/cool-sneakers

Getting the Dynamic Value

// app/products/[id]/page.tsx

export default function ProductPage({
  params
}: {
  params: { id: string }
}) {
  return (
    <h1>Product: {params.id}</h1>
  );
}

Visit /products/42 โ†’ Shows โ€œProduct: 42โ€ Visit /products/pizza โ†’ Shows โ€œProduct: pizzaโ€

One file. Infinite possibilities! ๐Ÿš€


๐ŸŽฃ Catch-all Routes: The Super Net

When Dynamic Routes Arenโ€™t Enough

What if your URL has multiple parts?

  • /docs/getting-started
  • /docs/api/hooks/useState
  • /docs/advanced/patterns/context/deep-dive

Each has a different number of segments. Dynamic routes canโ€™t handle this alone.

Enter the Triple Dot: [...slug]

app/
โ””โ”€โ”€ docs/
    โ””โ”€โ”€ [...slug]/
        โ””โ”€โ”€ page.tsx

The ... is like a fishing netโ€”it catches EVERYTHING after /docs/.

How It Works

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

export default function DocsPage({
  params
}: {
  params: { slug: string[] }
}) {
  // slug is an ARRAY of all segments
  return (
    <div>
      <p>Path: {params.slug.join(' โ†’ ')}</p>
    </div>
  );
}
URL params.slug
/docs/intro ['intro']
/docs/api/auth ['api', 'auth']
/docs/a/b/c/d ['a','b','c','d']

โš ๏ธ Important Catch

/docs alone wonโ€™t work with [...slug]. You need at least ONE segment after /docs.


๐ŸŽฏ Optional Catch-all Routes: The Friendly Net

The Problem with Catch-all

Remember: /docs alone doesnโ€™t work with [...slug].

But what if you WANT /docs to also show something?

The Solution: Double Brackets [[...slug]]

app/
โ””โ”€โ”€ docs/
    โ””โ”€โ”€ [[...slug]]/
        โ””โ”€โ”€ page.tsx

The extra brackets make the catch-all optional.

Now Everything Works!

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

export default function DocsPage({
  params
}: {
  params: { slug?: string[] }
}) {
  if (!params.slug) {
    return <h1>Welcome to Docs!</h1>;
  }

  return (
    <h1>Reading: {params.slug.join('/')}</h1>
  );
}
URL params.slug
/docs undefined
/docs/intro ['intro']
/docs/api/hooks ['api', 'hooks']

Best of both worlds! ๐ŸŽ‰


๐Ÿ“ Route Groups: The Invisible Organizers

The Messy Problem

Your app grows. Files everywhere:

app/
โ”œโ”€โ”€ login/
โ”œโ”€โ”€ register/
โ”œโ”€โ”€ forgot-password/
โ”œโ”€โ”€ dashboard/
โ”œโ”€โ”€ settings/
โ”œโ”€โ”€ profile/
โ””โ”€โ”€ ...20 more folders

Chaos! You want to organize these, but folders change URLsโ€ฆ

The Magic Parentheses: (groupName)

Folders with parentheses () are invisible in the URL!

app/
โ”œโ”€โ”€ (auth)/
โ”‚   โ”œโ”€โ”€ login/
โ”‚   โ”‚   โ””โ”€โ”€ page.tsx    โ†’ /login
โ”‚   โ”œโ”€โ”€ register/
โ”‚   โ”‚   โ””โ”€โ”€ page.tsx    โ†’ /register
โ”‚   โ””โ”€โ”€ forgot-password/
โ”‚       โ””โ”€โ”€ page.tsx    โ†’ /forgot-password
โ”‚
โ””โ”€โ”€ (dashboard)/
    โ”œโ”€โ”€ settings/
    โ”‚   โ””โ”€โ”€ page.tsx    โ†’ /settings
    โ””โ”€โ”€ profile/
        โ””โ”€โ”€ page.tsx    โ†’ /profile

Why This is Awesome

  1. Organization - Related pages grouped together
  2. Shared Layouts - Each group can have its own layout
  3. Clean URLs - (auth) doesnโ€™t appear in the address bar

Shared Layout Example

// app/(auth)/layout.tsx
// This layout wraps login, register, forgot-password

export default function AuthLayout({
  children
}: {
  children: React.ReactNode
}) {
  return (
    <div className="auth-container">
      <img src="/logo.png" alt="Logo" />
      {children}
    </div>
  );
}

All auth pages now share the same logo and containerโ€”without affecting their URLs!


๐Ÿ—บ๏ธ The Complete Picture

graph TD A[app/] --> B[page.tsx] A --> C[about/] C --> D[page.tsx] A --> E["[id]/"] E --> F[page.tsx] A --> G["[...slug]/"] G --> H[page.tsx] A --> I["[[...opt]]/"] I --> J[page.tsx] A --> K["#40;group#41;/"] K --> L[route/] L --> M[page.tsx] B --> B1["/"] D --> D1["/about"] F --> F1["/123, /abc"] H --> H1["/a/b/c"] J --> J1["/ or /x/y"] M --> M1["/route"]

๐ŸŽ“ Quick Reference

Pattern Folder Name Example URL Use Case
Static about/ /about Fixed pages
Dynamic [id]/ /products/42 Single variable
Catch-all [...slug]/ /docs/a/b/c Multiple segments
Optional [[...slug]]/ /docs or /docs/a/b Optional segments
Group (admin)/ URL unchanged Organization only

๐Ÿš€ You Did It!

You now understand Next.js file-based routing like a pro:

  1. Folders = URLs - Simple and intuitive
  2. page.tsx = Content - The welcome mat for each route
  3. [brackets] = Dynamic values
  4. [โ€ฆspread] = Catch multiple segments
  5. [[โ€ฆoptional]] = Catch OR skip
  6. (parentheses) = Invisible organization

The beauty of Next.js routing? Your file structure IS your sitemap. No config files. No router setup. Just folders and files, working together like a well-organized library.

Now go build something amazing! ๐ŸŽจโœจ

Loading story...

No Story Available

This concept doesn't have a story yet.

Story Preview

Story - Premium Content

Please sign in to view this concept and start learning.

Upgrade to Premium to unlock full access to all content.

Interactive Preview

Interactive - Premium Content

Please sign in to view this concept and start learning.

Upgrade to Premium to unlock full access to all content.

No Interactive Content

This concept doesn't have interactive content yet.

Cheatsheet Preview

Cheatsheet - Premium Content

Please sign in to view this concept and start learning.

Upgrade to Premium to unlock full access to all content.

No Cheatsheet Available

This concept doesn't have a cheatsheet yet.

Quiz Preview

Quiz - Premium Content

Please sign in to view this concept and start learning.

Upgrade to Premium to unlock full access to all content.

No Quiz Available

This concept doesn't have a quiz yet.