Discover how to keep your code neat and URLs clean without compromise!
Why Route groups with (groupName) in NextJS? - Purpose & Use Cases
Imagine building a website with many pages and trying to organize them by folders manually, but you want some pages grouped logically without changing their URL paths.
Manually managing folders for routes can clutter URLs or force awkward folder structures, making it hard to keep code clean and URLs user-friendly.
Route groups with (groupName) let you group pages in the file system for better code organization without affecting the URL structure, keeping URLs clean and code tidy.
app/dashboard/settings/page.tsx // URL: /dashboard/settings app/dashboard/profile/page.tsx // URL: /dashboard/profile
app/dashboard/(admin)/settings/page.tsx // URL: /dashboard/settings app/dashboard/(admin)/profile/page.tsx // URL: /dashboard/profile
This lets you organize your app's code clearly while keeping URLs simple and user-friendly.
Think of an online store where admin pages are grouped in code but don't show extra folder names in the URL, making links neat and easy to share.
Manual folder structure can clutter URLs and code.
Route groups organize code without changing URLs.
Clean URLs and tidy code make development easier and user experience better.
Practice
(groupName) in Next.js?Solution
Step 1: Understand route groups concept
Route groups use parentheses in folder names to group routes logically without affecting the URL path.Step 2: Compare URL effect
Since the URL does not include the group name, the main purpose is organization, not URL change.Final Answer:
To organize routes in folders without changing the URL structure -> Option AQuick Check:
Route groups = organize without URL change [OK]
- Thinking group name appears in URL
- Confusing with dynamic route parameters
- Assuming it adds middleware automatically
admin in Next.js?Solution
Step 1: Identify route group syntax
Route groups use parentheses around the folder name, like(groupName).Step 2: Match folder name
The folder named(admin)correctly creates a route group called admin.Final Answer:
(admin) -> Option BQuick Check:
Parentheses folder = route group [OK]
- Using square brackets which are for dynamic routes
- Using curly braces which are invalid
- Omitting parentheses and creating normal folders
app/(dashboard)/page.tsx
app/(dashboard)/settings/page.tsx
app/profile/page.tsx
What URL paths will be available?
Solution
Step 1: Understand route groups effect on URLs
Folders with parentheses group routes but do not add to URL path.Step 2: Map folder names to URLs
The(dashboard)group is invisible in URL, soapp/(dashboard)/page.tsxmaps to/because the page.tsx inside is the root of that group. Similarly,app/(dashboard)/settings/page.tsxmaps to/settings. Theapp/profile/page.tsxmaps to/profile.Final Answer:
/, /settings, /profile -> Option DQuick Check:
Route groups hide folder name but keep nested paths [OK]
- Including parentheses folder name in URL
- Ignoring nested folder names inside groups
- Assuming root group maps to root URL
(admin) inside app but your routes under it are not working. What is the most likely mistake?Solution
Step 1: Check route group folder requirements
Route groups require apage.tsxor similar file inside to define the route component.Step 2: Identify missing page file issue
If routes inside(admin)don't work, missingpage.tsxis the most common cause.Final Answer:
You forgot to add apage.tsxfile inside the(admin)folder -> Option AQuick Check:
Missing page.tsx = no route rendered [OK]
- Confusing folder naming syntax
- Placing folders outside app directory
- Assuming route groups auto-create pages
(marketing) and (sales) inside app with pages home.tsx and contact.tsx each. How should you organize folders so URLs are /home and /contact without group names in URL?Solution
Step 1: Use route groups to hide folder names
Folders named with parentheses group routes without adding to URL path.Step 2: Place pages inside separate route groups
Placinghome.tsxinside(marketing)andcontact.tsxinside(sales)keeps URLs as/homeand/contact.Final Answer:
app/(marketing)/home.tsxandapp/(sales)/contact.tsx-> Option CQuick Check:
Separate groups hide folder names, URLs stay clean [OK]
- Nesting groups causing longer URLs
- Placing pages outside groups losing organization
- Using page.tsx inconsistently inside groups
Ready to prove it?
Pass the test and this pattern moves unknown
