Next.js Routing
Routing in Next.js is a built-in system that automatically maps URLs to files and folders.
This means:
No need for extra libraries like React Router
Routes are created using file and folder structure
Navigation is fast and optimized
Next.js supports both pages/ and app/ directory-based routing.
Key Features of Next.js Routing
File-based routing system
Supports static, dynamic, and nested routes
Built-in navigation using Link
SEO-friendly URLs
High performance with prefetching
Types of Routes in Next.js
1. Static Routes
A static route is created using a fixed file name.
Example:
// pages/index.js
export default function Home() {
return <div>Home Page</div>;
}Access:
http://localhost:3000/2. Nested Routes
Nested routes are created using folders.
Example:
// pages/users/about.js
export default function About() {
return <div>About Page</div>;
}Access:
http://localhost:3000/users/about3. Dynamic Routes
Dynamic routes allow variable values in URLs.
Example:
// pages/users/[id].js
import { useRouter } from "next/router";
export default function User() {
const router = useRouter();
const { id } = router.query;
return <p>User: {id}</p>;
}Access:
http://localhost:3000/users/1
http://localhost:3000/users/abc4. API Routes
API routes allow backend logic inside Next.js.
Example:
// pages/api/hello.js
export default function handler(req, res) {
res.status(200).json({ message: "Hello, world!" });
}Access:
http://localhost:3000/api/helloAdvanced Routing
5. Catch-All Routes
Used to match multiple paths.
pages/[...slug].jsExample:
/blog/a
/blog/a/b
/blog/a/b/c6. Optional Catch-All Routes
Handles zero or more segments.
pages/[[...slug]].jsExample:
/blog
/blog/a
/blog/a/bNavigation in Next.js
Using Link Component
import Link from "next/link";
<Link href="/about">About Page</Link>Fast navigation
No page reload
Prefetching enabled
Using useRouter Hook
import { useRouter } from "next/router";
function MyComponent() {
const router = useRouter();
console.log(router.pathname);
console.log(router.query);
return <div>Check console</div>;
}Use cases:
Redirect user
Access query params
Handle dynamic routes
Step-by-Step Routing Setup
Step 1: Create Project
npx create-next-app myprojectStep 2: Create Structure
Inside pages/:
pages/
├── index.js
├── users/
│ ├── index.js
│ ├── about.js
│ └── [id].jsStep 3: Add Navigation
import Link from "next/link";
export default function Home() {
const id = 1;
return (
<>
<h1>Home Page</h1>
<ul>
<li><Link href="/users">Users</Link></li>
<li><Link href="/users/about">About</Link></li>
<li><Link href={`/users/${id}`}>User {id}</Link></li>
</ul>
</>
);
}Step 4: Run Project
npm run devReal-World Uses of Routing
Static websites
Blogs (categories, posts)
E-commerce (product pages)
Dashboards (profile, settings)
Full-stack apps (frontend + API)
Important Note
In Next.js 13+:
<Link>no longer needs<a>tag insideApp Router is preferred over Pages Router
Final Summary
Next.js routing is simple, powerful, and automatic. By just creating files and folders, you can build complete navigation systems without any external libraries.