Home >Web Front-end >JS Tutorial >A Quick Dive Into Route Groupings in Next.js
One of the coolest things I’ve recently learned while working on my Next.js project is route grouping.
It’s a simple yet powerful feature that lets you organize related routes without affecting the URL structure. If you haven’t explored this yet, let me break it down for you!
Think of route groups as a way to create clean, structured routes for your app without cluttering the URL paths. You do this by wrapping folders in parentheses ().
Let’s say you’re working on an e-commerce site. You might want all routes for products (like /products/shoes or /products/bags) to share some functionality, but you don’t want the word products to show in the URL.
A route group would handle this easily!
app/ (products)/ shoes/ page.tsx → /shoes bags/ page.tsx → /bags
I created two route groups in my current project: (auth) and (root).
I used route groups to define specific layouts for different app sections without duplicating code. For example:
I created two folders inside the app/ directory: (auth)and (root).
The app/(auth)/ Folder:
The _app/(root)/_ Folder:
But First Remove the Navbar from the Global Root Layout:
This is the central layout for the application.
It wraps everything in a theme provider so that features like theme toggling work seamlessly across all pages. It handles metadata and the global context for the app, ensuring the theme toggler affects all pages.
(root) "group" Layout(The actual root layout): Includes the Navbar to persist across the homepage and other main routes.
Excludes the Navbar but provides a layout specifically for the authentication pages.
Inside (auth), I created two folders: sign-in and sign-up. Each of these folders contains:
A page.tsx file for the respective page's content.
The (auth) layout wraps them up, ensuring that both pages share a common structure.
Well, for three reasons:
If you’re building a Next.js project, route groupings are worth exploring. They’re perfect for handling layouts dynamically while keeping your routes clean and organized.
Whether it’s to structure a homepage, authentication flow, or even admin dashboards, route groups allow you to create layouts that match your design needs.
The above is the detailed content of A Quick Dive Into Route Groupings in Next.js. For more information, please follow other related articles on the PHP Chinese website!