0
0
NextJSframework~8 mins

Multiple root layouts with route groups in NextJS - Performance & Optimization

Choose your learning style9 modes available
Performance: Multiple root layouts with route groups
MEDIUM IMPACT
This affects the initial page load speed and rendering performance by controlling how layouts and route groups are loaded and rendered.
Organizing routes with multiple root layouts and route groups to optimize loading
NextJS
app/(marketing)/layout.js
app/(marketing)/page.js
app/(dashboard)/layout.js
app/(dashboard)/page.js

// Using route groups with separate root layouts so only relevant layout code loads per route group.
Separates layout code by route groups, reducing bundle size and speeding up initial render.
📈 Performance GainSaves 50-100ms on LCP by loading smaller layout bundles.
Organizing routes with multiple root layouts and route groups to optimize loading
NextJS
app/layout.js
app/page.js
app/dashboard/layout.js
app/dashboard/page.js
app/settings/layout.js
app/settings/page.js

// All layouts are nested under a single root layout causing all layout code to load on every page.
All layout components load regardless of the route, increasing bundle size and delaying Largest Contentful Paint.
📉 Performance CostBlocks rendering for extra 100-200ms due to unnecessary layout code loading.
Performance Comparison
PatternDOM OperationsReflowsPaint CostVerdict
Single root layout for all routesHigh (all layouts rendered)Multiple reflows per page loadHigh paint cost due to large DOM[X] Bad
Multiple root layouts with route groupsLow (only relevant layouts rendered)Single reflow per route groupLower paint cost with smaller DOM[OK] Good
Rendering Pipeline
Multiple root layouts with route groups affect how the browser processes and paints the page by controlling which layout components are included in the render tree.
Style Calculation
Layout
Paint
Composite
⚠️ BottleneckLayout and Paint stages due to larger DOM and CSS from unnecessary layouts.
Core Web Vital Affected
LCP
This affects the initial page load speed and rendering performance by controlling how layouts and route groups are loaded and rendered.
Optimization Tips
1Use route groups to split layouts and reduce bundle size.
2Avoid nesting all routes under a single root layout to prevent loading unused code.
3Smaller layout bundles improve Largest Contentful Paint (LCP) and user experience.
Performance Quiz - 3 Questions
Test your performance knowledge
How do multiple root layouts with route groups affect the Largest Contentful Paint (LCP)?
AThey reduce LCP by loading only necessary layout code per route group.
BThey increase LCP by loading all layouts on every page.
CThey have no effect on LCP.
DThey delay LCP by adding extra network requests.
DevTools: Performance
How to check: Record a page load and inspect the Main thread for scripting and rendering times; check the Call Tree for layout and paint events.
What to look for: Look for reduced scripting and rendering times, fewer layout and paint events indicating optimized layout usage.