Pages and Routing in Next.js

πŸ“˜ Next.js πŸ‘ 32 views πŸ“… Dec 22, 2025
⏱ Estimated reading time: 2 min

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
Register Now

Share this Post


← Back to Tutorials

Popular Competitive Exam Quizzes