Multiple Root Layouts with Route Groups in Next.js
📖 Scenario: You are building a website with two main sections: a public blog and a private dashboard. Each section needs its own root layout to have different headers and footers.Using Next.js App Router, you will organize your routes with route groups and create multiple root layouts to keep the code clean and maintainable.
🎯 Goal: Create two separate root layouts for the blog and dashboard sections using route groups in Next.js. The blog layout will have a header with "Blog Header" and the dashboard layout will have a header with "Dashboard Header". Each layout will wrap its respective pages.
📋 What You'll Learn
Create a route group for the blog section with its own root layout
Create a route group for the dashboard section with its own root layout
Each root layout must include a header with the exact text: "Blog Header" or "Dashboard Header"
Create a page inside each route group that displays the exact text: "Welcome to the Blog" and "Welcome to the Dashboard" respectively
Use Next.js App Router conventions with functional components and React 18+
💡 Why This Matters
🌍 Real World
Many websites have different sections with unique layouts, such as public pages and user dashboards. Using multiple root layouts with route groups helps keep code organized and layouts consistent.
💼 Career
Understanding Next.js route groups and multiple root layouts is important for building scalable, maintainable web apps. This skill is valuable for frontend developers working with modern React frameworks.
Progress0 / 4 steps
1
Set up the blog route group with root layout
Create a folder named (blog) inside the app directory. Inside (blog), create a file named layout.tsx. In this file, write a React functional component called BlogLayout that returns a html element with a body containing a header with the text Blog Header. Export this component as default.
NextJS
Hint
Remember to export the layout component as default and include {children} inside the body to render nested pages.
2
Add a blog page inside the blog route group
Inside the (blog) folder, create a file named page.tsx. Write a React functional component called BlogPage that returns a main element with the text Welcome to the Blog. Export this component as default.
NextJS
Hint
Make sure the page component is exported as default and returns the correct text inside a main tag.
3
Set up the dashboard route group with root layout
Create a folder named (dashboard) inside the app directory. Inside (dashboard), create a file named layout.tsx. Write a React functional component called DashboardLayout that returns a html element with a body containing a header with the text Dashboard Header. Export this component as default.
NextJS
Hint
Remember to export the dashboard layout as default and include {children} inside the body.
4
Add a dashboard page inside the dashboard route group
Inside the (dashboard) folder, create a file named page.tsx. Write a React functional component called DashboardPage that returns a main element with the text Welcome to the Dashboard. Export this component as default.
NextJS
Hint
Make sure the dashboard page component is exported as default and returns the correct text inside a main tag.
Practice
(1/5)
1. What is the main purpose of using multiple root layouts in a Next.js app?
easy
A. To load all pages with the same layout
B. To change the URL structure automatically
C. To have different main designs for different parts of the app
D. To disable routing in some parts of the app
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 C
Quick Check:
Multiple root layouts = different main designs [OK]
Hint: Multiple root layouts mean different main designs [OK]
Common Mistakes:
Thinking multiple root layouts change URLs
Assuming all pages share one layout
Believing layouts disable routing
2. Which folder name correctly creates a route group without changing the URL in Next.js?
easy
A. dashboard/(admin)/settings
B. dashboard/admin/settings
C. dashboard/[admin]/settings
D. dashboard/{admin}/settings
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.
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 D
Quick Check:
URL '/' uses (marketing) layout [OK]
Hint: Root URL uses main route group's layout [OK]
Common Mistakes:
Assuming dashboard layout applies to '/'
Thinking layouts combine automatically
Believing no layout applies by default
4. You created a route group folder named (admin) but your app shows a 404 error when visiting /admin. What is the likely mistake?
medium
A. You forgot to restart the Next.js server
B. Route group folders do not change URLs, so /admin URL does not exist
C. You must add a dynamic route file inside (admin) folder
D. Parentheses in folder names are invalid syntax
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 B
Quick Check:
Route groups don't add URL segments [OK]
Hint: Route groups don't add URL parts [OK]
Common Mistakes:
Thinking (admin) folder creates /admin URL
Believing parentheses are invalid
Assuming dynamic routes are needed here
5. You want to create two separate main layouts: one for public pages and one for user dashboard pages. You also want URLs like /login and /dashboard/profile. How should you organize your folders using multiple root layouts and route groups?
hard
A. Create app/(public)/layout.js and app/(dashboard)/layout.js, put pages inside each, and use route groups to keep URLs clean
B. Create app/public/layout.js and app/dashboard/layout.js folders without parentheses
C. Put all pages in app/layout.js and use conditional rendering inside layout
D. Use dynamic routes like app/[section]/layout.js to switch layouts
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 A
Quick Check:
Route groups + multiple root layouts = clean URLs and separate designs [OK]
Hint: Use ( ) folders for layouts without changing URLs [OK]
Common Mistakes:
Not using parentheses, causing URL changes
Putting all pages under one layout losing separation
Using dynamic routes incorrectly for layouts
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.