Fundamentals
File Routing
Master file-based routing in Next.js, including static routes, dynamic segments, catch-all routes, and route groups.
1. Learning Objectives
By the end of this lesson, you will be able to:
Difficulty: Beginner.
2. Prerequisites
3. Overview
File routing means your filesystem is your route configuration. In the App Router, folders define URL segments and special files like page.tsx define what renders for that URL.
4. Why This Topic Matters
Routing is the skeleton of a web app. Good file routing keeps URLs predictable, code easy to find, and navigation simple to maintain as a product grows.
5. Real-World Analogy
File routing is like labeling drawers in a workshop. If the label says products/[id], everyone knows where product detail logic belongs and what shape the URL will have.
6. Core Concepts
| Pattern | Meaning | Example URL |
|---|---|---|
about/page.tsx | Static route | /about |
products/[id]/page.tsx | Dynamic segment | /products/42 |
docs/[...slug]/page.tsx | Catch-all route | /docs/a/b/c |
docs/[[...slug]]/page.tsx | Optional catch-all | /docs and /docs/a |
(marketing)/about/page.tsx | Route group | /about |
7. Syntax & API Reference
8. Visual Diagram
9. Live Example - Full Working Code
What just happened? A request to /blog/react-server-components passes { slug: "react-server-components" } into the page.
10. Interactive Playground
Try these route structures:
app/pricing/page.tsxapp/users/[userId]/page.tsxapp/docs/[...slug]/page.tsxThen visit the matching URLs and inspect the params.
11. Common Mistakes
| Mistake | Why It Happens | Correct Approach |
|---|---|---|
Using [id].tsx in App Router | This is a Pages Router habit. | Use [id]/page.tsx. |
| Expecting route groups to appear in URLs | Parentheses are organizational only. | Use groups for code organization, not path segments. |
| Forgetting catch-all params are arrays | Catch-all can match multiple path parts. | Type them as string[]. |
12. Best Practices
[productId].13. Browser Compatibility
| Feature | Browser Impact | Notes |
|---|---|---|
| Static routes | Server/framework feature | Browser receives normal HTML. |
| Dynamic routes | Server/framework feature | URL works like any route. |
| Catch-all routes | Server/framework feature | Useful for docs and CMS paths. |
14. Interview Questions
Easy: What doesapp/about/page.tsx create?
Answer: The /about route.
[id] mean in a route folder?
Answer: It is a dynamic segment that captures one part of the URL as a parameter.
Hard: Why use route groups?Answer: They organize route trees, layouts, and teams without adding extra path segments to the public URL.
15. Debugging Exercise
Broken structure:
Solution
Use app/products/[id]/page.tsx. In the App Router, dynamic segments are folders and the page file sits inside them.
16. Practice Exercises
/pricing./users/[userId]./docs/[[...slug]] and render a breadcrumb from params.17. Scenario-Based Challenge
A documentation site needs /docs, /docs/getting-started, and /docs/api/auth. Which route pattern fits?
Walkthrough
Use app/docs/[[...slug]]/page.tsx so the route can match both the docs index and nested documentation paths.
18. Quick Quiz
1. What folder creates a dynamic segment? Answer: [id].
2. What does [...slug] capture? Answer: One or more URL segments.
3. What does [[...slug]] capture? Answer: Zero or more URL segments.
4. Do route groups affect URLs? Answer: No.
5. What file renders UI for a route? Answer: page.tsx.
19. Summary & Key Takeaways
page.tsx exposes a route.20. Cheat Sheet
| Need | Structure |
|---|---|
/about | app/about/page.tsx |
/products/42 | app/products/[id]/page.tsx |
/docs/a/b | app/docs/[...slug]/page.tsx |
/docs and /docs/a | app/docs/[[...slug]]/page.tsx |
| Organization only | app/(marketing)/page.tsx |
21. Further Reading
22. Next Lesson Preview
Next, you will learn layouts: how shared UI wraps route segments, why layouts preserve state, and where global providers belong.