๐บ๏ธ 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/โ Homemysite.com/aboutโ About pagemysite.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
- Must be named
page.tsx- Other names wonโt work - Must export a default function - This is your component
- 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.tsxapp/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/1mysite.com/products/999mysite.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
- Organization - Related pages grouped together
- Shared Layouts - Each group can have its own layout
- 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:
- Folders = URLs - Simple and intuitive
- page.tsx = Content - The welcome mat for each route
- [brackets] = Dynamic values
- [โฆspread] = Catch multiple segments
- [[โฆoptional]] = Catch OR skip
- (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! ๐จโจ