App Router Navigation
App Router Structure and Navigation¶
The App Router in Next.js 13+ introduces a file-based routing system that organizes routes using a directory structure. This approach enables nested routing, dynamic segments, and seamless integration with API routes, forming the foundation for scalable, maintainable applications. This section explores how to implement these patterns effectively.
Nested Routing with Layouts¶
Nested routing in the App Router is achieved by organizing routes into a hierarchical directory structure. Each route can define a layout.js file to wrap child routes, creating a shared UI context.
Example: Nested Layout Structure¶
app/dashboard/layout.jswrapsdashboard/index.jsanddashboard/users/....app/dashboard/users/layout.jswraps user-specific pages.
Navigation Between Nested Routes¶
Use the useRouter hook to navigate programmatically:
import { useRouter } from 'next/navigation';
function Dashboard() {
const router = useRouter();
return (
<button onClick={() => router.push('/dashboard/users')}>
View Users
</button>
);
}
Diagram: Nested Route Hierarchy¶
app/
├── dashboard/
│ ├── layout.js
│ ├── index.js
│ └── users/
│ ├── layout.js
│ └── index.js
└── ...
Dynamic Segments¶
Dynamic segments allow routes to handle variable parameters, such as /posts/[id]. These segments are defined using square brackets in the route file name.
Example: Dynamic Post Route¶
- The
[id]segment captures the ID from the URL. - Access parameters via the
paramsobject:
Static Generation with generateStaticParams¶
For static routes, use generateStaticParams to pre-render dynamic segments:
export async function generateStaticParams() {
const posts = await fetchPosts(); // Fetch all post IDs
return posts.map(post => ({ id: post.id }));
}
API Route Integration¶
API routes in the App Router are defined in the app/api directory. These routes handle HTTP requests and can be used for server-side logic, data fetching, or authentication.
Example: Simple API Route¶
app/api/posts/route.jshandles requests to/api/posts.- Use the
Requestobject to parse incoming data:
Diagram: API Route Structure¶
Key takeaways¶
- Nested routing is achieved via directory hierarchies and
layout.jsfiles, enabling shared UI contexts. - Dynamic segments use
[id]notation to capture variable parameters, withparamsfor access. - API routes are defined in
app/api/, handling HTTP requests with server-side logic.