Fundamentals
App Router
Learn how the Next.js App Router maps folders to routes, composes layouts, and uses Server Components by default.
1. Learning Objectives
By the end of this lesson, you will be able to:
page.tsx, layout.tsx, loading.tsx, error.tsx, and route.ts.Difficulty: Beginner.
2. Prerequisites
3. Overview
The App Router is the modern Next.js routing system built around the app directory. Each folder is a route segment, and special files define pages, shared layouts, loading UI, error boundaries, and API endpoints.
4. Why This Topic Matters
The App Router is the default architecture for modern Next.js applications. It supports nested layouts, streaming, Server Components, server data fetching, and co-located route behavior, which makes large applications easier to organize.
5. Real-World Analogy
Think of the App Router like a building map. Folders are floors and rooms; layout.tsx is the hallway shared by rooms; page.tsx is the room itself; loading.tsx is the sign shown while the room is being prepared.
6. Core Concepts
| File | Purpose |
|---|---|
page.tsx | Makes a route publicly accessible. |
layout.tsx | Shared UI that wraps child routes and preserves state. |
template.tsx | Shared UI that remounts on navigation. |
loading.tsx | Suspense fallback for a segment. |
error.tsx | Client-side error boundary for a segment. |
not-found.tsx | Segment-level 404 UI. |
route.ts | HTTP endpoint for APIs and webhooks. |
7. Syntax & API Reference
8. Visual Diagram
9. Live Example - Full Working Code
What just happened? Visiting /dashboard renders the root layout, then the dashboard layout, then the dashboard page.
10. Interactive Playground
Create a local App Router project and try:
app/dashboard/settings/page.tsx.app/dashboard/loading.tsx.11. Common Mistakes
| Mistake | Why It Happens | Correct Approach |
|---|---|---|
Forgetting page.tsx | A folder alone does not create a public route. | Add page.tsx for routes users can visit. |
| Putting stateful UI in a Server Component | App Router components are server-first. | Move stateful parts into a child Client Component. |
| Expecting layouts to remount on every navigation | Layouts preserve state by design. | Use template.tsx when remounting is needed. |
12. Best Practices
13. Browser Compatibility
| Feature | Browser Impact | Notes |
|---|---|---|
| Server Components | Server-side | Browser receives rendered output and client payload. |
| Loading UI | React Suspense | Supported through framework rendering. |
| Error boundaries | Client-side reset UI | error.tsx must be a Client Component. |
14. Interview Questions
Easy: What file makes an App Router route public?Answer: page.tsx.
layout.tsx and template.tsx?
Answer: A layout persists across navigation within its segment, while a template remounts when navigation happens.
Hard: Why does the App Router make Server Components the default?Answer: Server Components reduce shipped JavaScript, allow server-side data access, improve first render, and keep sensitive logic out of the browser.
15. Debugging Exercise
Broken structure:
Solution
Rename index.tsx to page.tsx. The App Router uses special file names, not the Pages Router index convention.
16. Practice Exercises
/dashboard./dashboard/settings with a shared dashboard layout.17. Scenario-Based Challenge
A SaaS app needs /app/billing, /app/team, and /app/settings to share the same sidebar. How should you structure routes?
Walkthrough
Create app/app/layout.tsx for the shared shell and place billing/page.tsx, team/page.tsx, and settings/page.tsx inside that segment.
18. Quick Quiz
1. What is a route segment? Answer: A folder in the route tree.
2. What file defines loading UI? Answer: loading.tsx.
3. What file defines an HTTP endpoint? Answer: route.ts.
4. Do layouts preserve state? Answer: Yes.
5. What creates a route without affecting URL path? Answer: A route group.
19. Summary & Key Takeaways
page.tsx creates public routes.20. Cheat Sheet
| Need | File |
|---|---|
| Public UI route | page.tsx |
| Shared shell | layout.tsx |
| Remounting shell | template.tsx |
| Pending state | loading.tsx |
| Segment error UI | error.tsx |
| API endpoint | route.ts |
21. Further Reading
22. Next Lesson Preview
Next, you will compare the App Router with the older Pages Router so you know how to read legacy codebases and plan migrations.