Last Updated : 23 Jul, 2025
Next.js is a powerful framework built on top of React that simplifies server-side rendering, static site generation, and routing. In this article, we'll learn about the fundamentals of Next.js routing, explore dynamic and nested routes, and see how to handle custom routes and API routes.
What is Next.js Routing?Next.js offers a simple and intuitive way to manage routing using a file-based system. Each file in the pages
directory automatically becomes a route, eliminating the need for complex routing configurations.
All the files in our pages directory having .js, .jsx, .ts and .tsx are automatically routed. The index.js is the root directory. For Example: If we create a file in the pages directory named index.js. Then it could be accessed by going to http://localhost:3000/
// pages/index.js.jsNested Routesconst Home = () => {
return(
<div>
Home Page
</div>
);
}
export default Home;
If we create a nested folder structure, our routes will also be structured in the same manner. For Example: If we create a new folder called users and create a new file called about.js within it, we can access this file by visiting http://localhost:3000/users/about
// pages/user/About.js const About = () => { return( <div> About Page </div> ); } export default About;Dynamic Routes
We can also accept URL parameters and create dynamic routes using the bracket syntax. For Example: If we create a new page in the pages directory called [id].js then the component exported from this file, could access the parameter id and render content accordingly. This can be accessed by going to localhost:3000/<Any Dynamic Id>.
// pages/users/[id].jsAPI Routesimport { useRouter } from 'next/router'; const User= () => { const router = useRouter(); const { id } = router.query; return <p>User: {id}</p>; }; export default User;
Next.js supports creating API Routes/endpoints by adding files under the pages/api directory. Each file in this directory is mapped to an API endpoint.
Example:
// Filename : pages/api/hello.js export default function handler(req, res) { res.status(200).json({ message: 'Hello, world!' }); }Advanced Routing Catch-All Routes
We can catch all paths in Next.js using catch-all routes. For this, we have to add three dots inside the square brackets in the name of the file as shown below:
./pages/[...file_name].jsOptional Catch-All Routes
Optional catch-all routes in Next.js extend the concept of catch-all routes by allowing you to handle routes with a variable number of segments, including the option of no segments at all.
We can make catch-all routes optional in NextJS using optional catch-all routes. For this, we have to add three dots inside the double square brackets in the name of the file. For example:-
./pages/[[...file_name]].jsLinking Between Pages
We can navigate between pages using the Link component from the next/link module. This component enables client-side navigation between pages in the NextJS application. It provides a smoother user experience compared to traditional full-page reloads.
useRouter hookThe useRouter hook allows you to access the Next.js router object and obtain information about the current route, query parameters, and other route-related details.
import { useRouter } from 'next/router '; function MyComponent() { //Main Syntax const router = useRouter(); // Accessing route information using router object console.log(router.pathname); // Current route console.log(router.query); // Query parameters return ( // Your component JSX ); }Steps to Implement Next.js Routing Step 1: Run the following command to Create a new Next Application.
npx create-next-app myproject
When we open our app in a code editor we see the following project structure.
Project Structure: Next.js Folder StructureFor the scope of this tutorial, we will only focus on the pages directory. When we initialize our Next App, we get a default index route. It works as a homepage for our application. Now we'll set up three different routes to test all the route types in Next Js.
Make a new folder called users in the pages directory called users, then make three new files in the user's folder: [id].js, index.js, and about.js. We will also use the Link component to create navigation on our homepage to access these routes.
JavaScript
//pages/index.js
import React from "react";
import Link from "next/link";
const HomePage = () => {
// This is id for dynamic route, you
// can change it to any value.
const id = 1;
return (
<>
<h1>Home Page</h1>
<ul>
<li>
<Link href="/users">
<a>Users</a>
</Link>
</li>
<li>
<Link href="/users/about">
<a>About Users</a>
</Link>
</li>
<li>
<Link href={`/users/${id}`}>
<a>User with id {id}</a>
</Link>
</li>
</ul>
</>
);
};
export default HomePage;
JavaScript
//pages/users/index.js
import React from "react";
const Users = () => {
return <h1>Users Page</h1>;
};
export default Users;
JavaScript
//pages/users/about.js
import React from 'react'
const Users = () => {
return (
<h1>Users About Page</h1>
)
}
export default Users
JavaScript
//pages/users/[id].js
import React from 'react'
import { useRouter } from 'next/router'
const User = () => {
const router = useRouter()
return
<h1>
User with id
{router.query.id}
</h1>
}
export default User;
Step to run the application: Run your Next.js app using the following command:
npm run devOutput Features of Next.js Routing
pages
directory corresponds to a route in the application. This makes it easy to manage routes without complex configurations.pages
directory create nested routes, making it easy to organize and manage complex route hierarchies.Link
component enables fast client-side navigation with prefetching capabilities, improving the performance and user experience of your application.next.config.js
file, allowing you to create more flexible and SEO-friendly URLs.Next.js's file-system-based routing is powerful and intuitive, allowing developers to create static, dynamic, nested, and API routes effortlessly. With support for advanced features like catch-all routes and a built-in Link component for navigation, Next.js simplifies the process of building scalable and high-performance web applications.
RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4