Next.js 13+ introduced the revolutionary App Router, replacing the traditional Pages Router with a more powerful, file-system based routing system. This post will guide you through its core concepts with practical examples.
The App Router uses a app/ directory where each folder represents a route segment. A page.js file inside defines the route's UI.
// app/dashboard/page.js
export default function Dashboard() {
return <h1>Dashboard Page</h1>
}
Create dynamic routes by wrapping folder names in square brackets [param]. Access params via the params prop.
// app/blog/[slug]/page.js
export default function BlogPost({ params }) {
return <h1>Blog Post: {params.slug}</h1>
}
Layouts persist across routes while templates create new instances. Define them with layout.js and template.js.
// app/dashboard/layout.js
export default function DashboardLayout({ children }) {
return (
<div>
<Sidebar />
<main>{children}</main>
</div>
)
}
The App Router introduces a new filesystem-based routing system. Here's the standard project structure for a Next.js app using the App Router: Key Features of This Structure: 1. Route Segments: Each folder in app/ becomes a URL segment 2. Special Files: page.js, layout.js, loading.js, etc. have specific meanings 3. Nesting: Folders create nested routes automatically 4. Convention-based: No configuration needed for basic routing
my-next-app/
├── app/
│ ├── (auth)/ # Route group (doesn't appear in URL)
│ │ ├── login/
│ │ │ └── page.js # example.com/login
│ │ └── register/
│ │ └── page.js # example.com/register
│ ├── blog/
│ │ ├── [slug]/ # Dynamic route
│ │ │ └── page.js # example.com/blog/hello-world
│ │ └── layout.js # Shared layout for all blog routes
│ ├── dashboard/
│ │ ├── analytics/
│ │ │ └── page.js # example.com/dashboard/analytics
│ │ ├── settings/
│ │ │ └── page.js # example.com/dashboard/settings
│ │ └── layout.js # Dashboard-specific layout
│ ├── @modal/ # Parallel route (optional)
│ │ └── page.js
│ ├── favicon.ico
│ ├── globals.css
│ ├── layout.js # Root layout (required)
│ └── template.js # Optional root template
├── public/ # Static files
│ └── images/
├── next.config.js # Next.js configuration
└── package.json
The App Router brings significant improvements in routing, data fetching, and performance. Start migrating your projects today to benefit from its modern features.
Likes • 12 Views • Apr 7, 2025