- Next.js
- App Router File Conventions
- Route Groups and Multiple Root Layouts
Route Groups and Multiple Root Layouts
Route groups wrap folder names in parentheses, such as (marketing) and (dashboard), organizing files and layouts without affecting the public URL path. They allow developers to create multiple root layouts with completely different <html> and <body> tags in the same application.
Learning Objectives
Organize complex codebases using parentheses syntax (folder) without altering URLs.
Implement multiple independent root layouts for distinct application surfaces.
Understand navigation transitions between different root layouts (full page reloads).
Prevent route resolution collisions when using route groups.
Prerequisites & Background Knowledge
Why Interviewers Care
Standard architectural question when designing applications with distinct public marketing sites and logged-in consoles.
The Challenge
How do route groups (folder) work in Next.js App Router? How do you configure multiple root layouts for public marketing and authenticated console surfaces within the same codebase?
Code Architecture & Implementation
1// File Structure:2// app/3// ├── (marketing)/4// │ ├── layout.tsx <- Root Layout 1 (Public marketing navbar & footer)5// │ ├── page.tsx <- Maps to URL: /6// │ └── pricing/7// │ └── page.tsx <- Maps to URL: /pricing8// └── (dashboard)/9// ├── layout.tsx <- Root Layout 2 (Authenticated sidebar & console header)10// └── console/11// └── page.tsx <- Maps to URL: /console1213// app/(marketing)/layout.tsx14export default function MarketingRootLayout({ children }: { children: React.ReactNode }) {15 return (16 <html lang="en">17 <body className="marketing-theme">18 <header className="public-nav">Brand Marketing Nav</header>19 {children}20 <footer>Marketing Footer</footer>21 </body>22 </html>23 );24}2526// app/(dashboard)/layout.tsx27export default function DashboardRootLayout({ children }: { children: React.ReactNode }) {28 return (29 <html lang="en">30 <body className="dashboard-console-theme">31 <aside className="admin-sidebar">Console Nav</aside>32 <div className="console-content">{children}</div>33 </body>34 </html>35 );36}
Architectural Explanation
Route Groups Syntax & Mechanics
Wrapping a folder name in parentheses (groupName) instructs the Next.js router to omit that folder segment from the URL path:
app/(marketing)/about/page.tsx$\rightarrow$/aboutapp/(shop)/cart/page.tsx$\rightarrow$/cart
Multiple Root Layouts
By deleting the top-level app/layout.tsx file, developers can define separate root layouts inside top-level route groups:
- Surface Isolation:
app/(marketing)/layout.tsxcan render public headers, Google Analytics tags, and marketing stylesheets. - Console Isolation:
app/(admin)/layout.tsxcan load an entirely different authenticated theme, dark mode defaults, and enterprise toolbars. - Caveat on Navigation: Navigating between routes governed by different root layouts triggers a full document reload (hard navigation) rather than a soft client transition, because the entire
<html>and<body>DOM structure changes.
Common Mistakes & Anti-Patterns
Creating route collisions, such as defining app/(marketing)/about/page.tsx and app/(shop)/about/page.tsx (causes build error: multiple routes resolve to /about).
Forgetting that every root layout in a multi-root application must define its own <html> and <body> tags.
Expecting soft client-side transitions when navigating across different root layout boundaries.
Interview Follow-Up Traps
Interviewer asks: "If you have multiple root layouts, does navigating between them preserve React client state?" Trap answer: Yes, Next.js handles it. Correct answer: No! Navigating across different root layouts triggers a full browser reload because the root <html> and <body> nodes are replaced.
Interviewer asks: "Can you nest route groups inside other route groups?" Trap answer: No, only at the root. Correct answer: Yes, route groups can be nested at any depth in the folder tree to organize layouts or middleware matcher groupings.
Frequently Asked Questions
Why would an enterprise use route groups instead of multiple subdomains?
To share components, TypeScript types, and server actions within a single unified codebase while maintaining distinct layout visual hierarchies.
Can you apply middleware matching based on route groups?
No directly, because route group folder names are omitted from the request URL pathname that middleware inspects.
How do you name a route group folder?
Wrap any descriptive name in parentheses: (auth), (dashboard), (shop).
What happens if two route groups define the same path?
Next.js throws a build-time route collision error during compilation.
