One of the standout features of Next.js is its file-based routing system. Unlike traditional React applications that require manual route configuration, Next.js automatically generates routes based on the file structure in the pages
directory. This simplifies route management and makes it easier to build complex applications. In this article, we’ll explore how file-based routing works in Next.js and how you can use it to create dynamic and nested routes.
How File-based Routing Works
In Next.js, the pages
directory is the heart of your application’s routing system. Each file in this directory corresponds to a route in your application. For example:
pages/index.js
corresponds to the root route (/
).pages/about.js
corresponds to the/about
route.pages/blog/index.js
corresponds to the/blog
route.
This file-based approach eliminates the need for manual route configuration and makes it easy to organize your application’s routes.
Creating Basic Routes
To create a basic route, simply add a new file to the pages
directory. For example, to create an /about
route, add a file named about.js
:
// pages/about.js
export default function About() {
return (
<div>
<h1>About Us</h1>
<p>Learn more about our company.</p>
</div>
);
}
This file will automatically be accessible at /about
.
Creating Nested Routes
Nested routes are created by organizing files into subdirectories within the pages
directory. For example, to create a /blog
route with nested routes like /blog/first-post
and /blog/second-post
, you can organize your files like this:
pages/
├── blog/
│ ├── first-post.js
│ ├── second-post.js
│ └── index.js
└── index.js
The index.js
file in the blog
directory corresponds to the /blog
route, while first-post.js
and second-post.js
correspond to /blog/first-post
and /blog/second-post
, respectively.
Creating Dynamic Routes
Dynamic routes allow you to handle routes with dynamic parameters, such as /blog/[slug]
. To create a dynamic route, use square brackets in the file name. For example, to create a dynamic route for blog posts, add a file named [slug].js
in the pages/blog
directory:
// pages/blog/[slug].js
import { useRouter } from 'next/router';
export default function BlogPost() {
const router = useRouter();
const { slug } = router.query;
return (
<div>
<h1>Blog Post: {slug}</h1>
<p>This is the content for the blog post titled "{slug}".</p>
</div>
);
}
This file will handle routes like /blog/first-post
and /blog/second-post
, where slug
is a dynamic parameter.
Linking Between Pages
Next.js provides a Link
component for client-side navigation between pages. This ensures that page transitions are fast and smooth, without a full page reload.
import Link from 'next/link';
export default function Home() {
return (
<div>
<h1>Welcome to My Next.js App!</h1>
<Link href="/about">
<a>About Us</a>
</Link>
</div>
);
}
Secrets and Hidden Facts
- Catch-All Routes: Next.js supports catch-all routes using
[...slug].js
, which can match multiple segments of a route. - Optional Catch-All Routes: Use
[[...slug]].js
to create optional catch-all routes. - Custom 404 Page: Create a
404.js
file in thepages
directory to customize the 404 error page.
Conclusion
File-based routing is one of the most powerful features of Next.js, simplifying route management and making it easy to build complex applications. Whether you’re creating basic routes, nested routes, or dynamic routes, Next.js provides a flexible and intuitive system for managing your application’s routes. By leveraging file-based routing, you can build scalable and maintainable applications with ease.

No comments: