Pages and Routing in Next.js
β± 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
2. Nested Routes
You can create nested routes by nesting folders.
3. Dynamic Routes
Dynamic routes are created using square brackets [].
Example
Here, slug is a dynamic parameter.
4. Route Groups
Route groups help organize routes without affecting the URL structure.
5. Layouts in Routing
Layouts allow you to share UI across multiple pages.
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.
7. Not Found Pages
You can create a custom 404 page using not-found.js.
8. Pages Router (Older Method)
Older versions use the pages/ directory.
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.
Register Now
Share this Post
β Back to Tutorials