Pages and Routing in Next.js

Next.js 128 views Dec 22, 2025 2 min read

Next.js provides a powerful and simple routing system based on the file and folder structure of your project. You donโ€™t need to configure routes manuallyโ€”routes are created automatically.


1. Routing in Next.js (App Router)

Next.js uses the App Router (app/ directory) in modern versions. Each folder represents a route, and each page.js file represents a page.

Example

app/ โ”œโ”€โ”€ page.js โ†’ / โ”œโ”€โ”€ about/ โ”‚ โ””โ”€โ”€ page.js โ†’ /about โ”œโ”€โ”€ contact/ โ”‚ โ””โ”€โ”€ page.js โ†’ /contact

2. Nested Routes

You can create nested routes by nesting folders.

app/ โ”œโ”€โ”€ blog/ โ”‚ โ”œโ”€โ”€ page.js โ†’ /blog โ”‚ โ””โ”€โ”€ post/ โ”‚ โ””โ”€โ”€ page.js โ†’ /blog/post

3. Dynamic Routes

Dynamic routes are created using square brackets [].

Example

app/ โ”œโ”€โ”€ blog/ โ”‚ โ””โ”€โ”€ [slug]/ โ”‚ โ””โ”€โ”€ page.js โ†’ /blog/my-first-post

Here, slug is a dynamic parameter.


4. Route Groups

Route groups help organize routes without affecting the URL structure.

app/ โ”œโ”€โ”€ (auth)/ โ”‚ โ”œโ”€โ”€ login/ โ”‚ โ”‚ โ””โ”€โ”€ page.js โ†’ /login โ”‚ โ””โ”€โ”€ register/ โ”‚ โ””โ”€โ”€ page.js โ†’ /register

5. Layouts in Routing

Layouts allow you to share UI across multiple pages.

app/ โ”œโ”€โ”€ dashboard/ โ”‚ โ”œโ”€โ”€ layout.js โ”‚ โ””โ”€โ”€ page.js

The layout.js file wraps all pages inside the dashboard route.


6. Navigation Between Pages

Next.js provides the Link component for client-side navigation.

import Link from 'next/link'; export default function Home() { return ( <Link href="/about">Go to About Page</Link> ); }

7. Not Found Pages

You can create a custom 404 page using not-found.js.

app/ โ””โ”€โ”€ not-found.js

8. Pages Router (Older Method)

Older versions use the pages/ directory.

pages/ โ”œโ”€โ”€ index.js โ†’ / โ”œโ”€โ”€ about.js โ†’ /about โ”œโ”€โ”€ blog/ โ”‚ โ””โ”€โ”€ [slug].js โ†’ /blog/post-name

For new projects, the App Router is recommended.


Conclusion

Next.js routing is simple, powerful, and scalable. By using file-based routing, layouts, and dynamic routes, you can easily build complex navigation structures without manual configuration.

Some advanced sections are available for Registered Members
Share this Post
๐Ÿš€ Want to Test Your Knowledge?

Take quizzes related to this topic and see where you stand!

Start Quiz Now
Back to Tutorials