Multiple root layouts let you organize different parts of your app with their own main design. Route groups help keep URLs clean while grouping pages under these layouts.
Multiple root layouts with route groups in NextJS
app/
(group1)/
layout.tsx
page.tsx
(group2)/
layout.tsx
dashboard/
page.tsxRoute groups are folders wrapped in parentheses, like (group1).
Each group can have its own layout.tsx file that acts as a root layout for pages inside it.
app/
(marketing)/
layout.tsx
page.tsx
(dashboard)/
layout.tsx
dashboard/
page.tsx(admin) group has a root layout shared by all admin pages like users and settings.app/
(admin)/
layout.tsx
users/
page.tsx
settings/
page.tsxThis example shows two route groups: (marketing) and (dashboard). The (marketing) group handles the root path / with its layout. The (dashboard) group handles /dashboard with its own layout. Each has different colors and headers. The pages inside show different welcome messages.
/* app/(marketing)/layout.tsx */ import React from 'react'; export default function MarketingLayout({ children }: { children: React.ReactNode }) { return ( <html lang="en"> <body> <header style={{ backgroundColor: '#a0d8f1', padding: '1rem' }}> <h1>Marketing Site</h1> </header> <main>{children}</main> <footer style={{ backgroundColor: '#a0d8f1', padding: '1rem' }}> <p>Ā© 2024 Marketing</p> </footer> </body> </html> ); } /* app/(marketing)/page.tsx */ export default function MarketingPage() { return <p>Welcome to the Marketing homepage!</p>; } /* app/(dashboard)/layout.tsx */ import React from 'react'; export default function DashboardLayout({ children }: { children: React.ReactNode }) { return ( <html lang="en"> <body> <nav style={{ backgroundColor: '#f1c40f', padding: '1rem' }}> <h1>Dashboard</h1> </nav> <section>{children}</section> </body> </html> ); } /* app/(dashboard)/dashboard/page.tsx */ export default function DashboardPage() { return <p>Welcome to your Dashboard!</p>; }
Route groups do not change the URL path. They only organize files and layouts.
Use route groups to keep your app structure clean and maintainable.
Each route group can have its own layout.tsx that wraps all pages inside it.
Multiple root layouts let you have different main designs for different parts of your app.
Route groups use parentheses in folder names to group routes without changing URLs.
This helps keep your app organized and lets you reuse layouts easily.
Practice
Solution
Step 1: Understand root layouts
Root layouts define the main design or structure for pages in a Next.js app.Step 2: Purpose of multiple root layouts
Using multiple root layouts allows different parts of the app to have their own main design, improving organization and user experience.Final Answer:
To have different main designs for different parts of the app -> Option CQuick Check:
Multiple root layouts = different main designs [OK]
- Thinking multiple root layouts change URLs
- Assuming all pages share one layout
- Believing layouts disable routing
Solution
Step 1: Identify route group syntax
Route groups use parentheses in folder names to group routes without affecting URLs.Step 2: Check options
Only dashboard/(admin)/settings uses parentheses correctly: (admin). Other options use brackets or braces which are for dynamic routes or invalid.Final Answer:
dashboard/(admin)/settings -> Option AQuick Check:
Route groups use parentheses ( ) [OK]
- Using square brackets [] for route groups
- Using curly braces {} which are invalid
- Confusing dynamic routes with route groups
app/
(marketing)/
layout.js
page.js
(dashboard)/
layout.js
dashboard/page.js
What layout will the page at URL / use?Solution
Step 1: Understand route groups and root layouts
Route groups like (marketing) and (dashboard) separate parts of the app with their own layouts.Step 2: Determine which route group matches URL '/'
The root URL '/' matches the (marketing) group because it is the default or main group for the homepage.Final Answer:
The layout from (marketing) folder -> Option DQuick Check:
URL '/' uses (marketing) layout [OK]
- Assuming dashboard layout applies to '/'
- Thinking layouts combine automatically
- Believing no layout applies by default
(admin) but your app shows a 404 error when visiting /admin. What is the likely mistake?Solution
Step 1: Understand route groups and URLs
Route groups using parentheses group routes but do not add to the URL path.Step 2: Check URL vs folder name
The folder (admin) does not create /admin URL. To have /admin URL, folder must be named 'admin' without parentheses.Final Answer:
Route group folders do not change URLs, so /admin URL does not exist -> Option BQuick Check:
Route groups don't add URL segments [OK]
- Thinking (admin) folder creates /admin URL
- Believing parentheses are invalid
- Assuming dynamic routes are needed here
/login and /dashboard/profile. How should you organize your folders using multiple root layouts and route groups?Solution
Step 1: Use route groups for separate layouts
Route groups with parentheses let you organize folders without affecting URLs, perfect for multiple root layouts.Step 2: Organize folders for public and dashboard
Creating(public)and(dashboard)folders with their own layouts keeps code clean and URLs like /login and /dashboard/profile work as expected.Final Answer:
Create app/(public)/layout.js and app/(dashboard)/layout.js, put pages inside each, and use route groups to keep URLs clean -> Option AQuick Check:
Route groups + multiple root layouts = clean URLs and separate designs [OK]
- Not using parentheses, causing URL changes
- Putting all pages under one layout losing separation
- Using dynamic routes incorrectly for layouts
Ready to prove it?
Pass the test and this pattern moves unknown
