Nested Layouts, Route Groups, and Parallel Routes
The difference between a site that feels like an application and one that feels like a slideshow is almost entirely about what survives a navigation. In a well-built dashboard the sidebar stays put, the unread badge keeps its count, the scroll position of the nav does not reset, and only the content column changes. The App Router gives you that behaviour by default through layouts, and then gives you three sharper tools â nesting, route groups, and parallel slots â for the cases where one shared shell is not enough.
This lesson explains how a layout persists across client-side navigation, how nested layouts compose, why route groups let you reorganise files without changing a single URL, and when parallel routes are the right answer rather than a clever one.
Prerequisites
- !Page, layout and folder conventions in the app directory
- !The difference between Server and Client Components
- !A Next.js project with at least two nested routes
What You'll Learn
- 1Explain what state survives a layout during navigation
- 2Compose two or more nested layouts without duplicating chrome
- 3Apply route groups to split sections that need different shells
- 4Use parallel slots for hard cases such as modal-over-list views
What a layout actually persists
A layout receives children and renders it once for every route underneath its folder. When the user navigates between two sibling pages, the router replaces only the children value â the layout component is not unmounted, its useState values survive, its effects are not re-run, and the DOM nodes it owns stay in place. That single fact explains the behaviour you get for free: a video player in the shell keeps playing, a form half-filled in a sidebar keeps its value, and scroll restoration applies to the content region rather than the whole document.
The corollary is just as important. Anything you put in a layout is re-rendered when its props change, but it is not re-fetched per navigation unless the data itself is dynamic. A layout that awaits a database call on every request pays that cost for every child route, so expensive lookups belong in pages that actually need them.
// app/(app)/layout.js â the persistent shell for the signed-in area
import Link from 'next/link';
import { getUser } from '@/lib/session';
import UnreadBadge from './unread-badge'; // Client Component
export default async function AppLayout({ children }) {
const user = await getUser();
return (
<div className="app-shell">
<aside>
<Link href="/overview">Overview</Link>
<Link href="/reports">Reports</Link>
<Link href="/settings">
Settings <UnreadBadge />
</Link>
</aside>
<main>{children}</main>
<footer>Signed in as {user.email}</footer>
</div>
);
}
Because UnreadBadge is a Client Component rendered by a Server layout, it ships its own small bundle and hydrates independently. The layout around it stays on the server, so the user's email never appears in a client-visible JavaScript payload.
Nesting shells instead of repeating them
Folders nest, so layouts nest. app/(app)/layout.js wraps everything inside the group, and app/(app)/reports/layout.js wraps only the reports section. Each level receives the already-wrapped output of the level above as children, which means the inner layout never re-renders the outer one and never duplicates navigation markup.
// app/(app)/reports/layout.js â a second, narrower shell
import { getReportNav } from '@/lib/reports';
import ReportNav from './report-nav';
export default async function ReportsLayout({ children }) {
const sections = await getReportNav();
return (
<div className="reports-grid">
<nav>
<ReportNav sections={sections} />
</nav>
<section>{children}</section>
</div>
);
}
With both files in place, /reports renders the outer sidebar, the reports sub-nav, and the page. Navigating from /reports/weekly to /reports/monthly re-renders only the leaf: both layouts hold their position. Navigating from /reports/weekly to /overview keeps the outer layout and swaps out the entire reports subtree. There is no configuration for this â it falls out of the tree structure.
One boundary rule keeps the mental model honest: loading.js and error.js placed in a folder suspend or catch only what is inside that folder. A loading.js next to reports/layout.js shows its skeleton while the reports subtree loads, but the outer sidebar renders immediately. That locality is why nested shells feel fast.
Templates: the same shell, freshly mounted
Occasionally you want the opposite of persistence. A marketing page that should play its entrance animation every time, a wizard whose progress indicator must reset, a third-party widget that caches internal state and misbehaves when it is reused â all of these need a component that is torn down and rebuilt on each navigation. template.js is that component: it takes the same children as a layout, sits at the same position in the tree, and re-mounts for every route beneath it.
// app/(marketing)/template.js
export default function MarketingTemplate({ children }) {
// A new instance per navigation: enter animations replay every time.
return <div className="animate-page-in">{children}</div>;
}
The rule of thumb is straightforward. Put anything whose state should survive navigation in layout.js and anything whose state should not in template.js. Nesting works the same way â a template wraps the children of its segment, and layouts above it keep their own persistence. What does not work is mixing both in one folder: layout.js and template.js can coexist, but then the layout persists while the template re-mounts beneath it, which is exactly what you want only when you have deliberately asked for both.
Route groups reorganise files, not URLs
A folder wrapped in parentheses â (marketing), (app), (auth) â is a route group. Next.js treats it as structure for you and pretends it does not exist when matching URLs. The payoff is that two sections can have entirely different layouts, fonts, and metadata without either appearing in a path.
| Folder | URL served | Why you would do it |
|---|---|---|
app/(marketing)/pricing/page.js | /pricing | Marketing pages get a wide, public shell |
app/(app)/pricing/page.js | not valid | Two groups cannot own the same URL â one wins at build |
app/(auth)/login/page.js | /login | Auth screens drop the app chrome entirely |
app/(app)/settings/page.js | /settings | Signed-in pages keep the sidebar |
Two groups resolving to the same URL is a real conflict rather than a stylistic choice, and Next.js will fail the build or pick one deterministically depending on version. The practical rule is that a URL belongs to exactly one group; if you need the same content in two shells, render it once and wrap it differently rather than duplicating the route.
Groups also carry shared context. A provider that must wrap a whole section â a theme controller, a query client, a toast host â belongs in that section's layout.js, because layouts are the only files in the tree that legitimately wrap their siblings' children.
Parallel routes render several trees at once
Sometimes one URL needs more than one independent subtree: a dashboard that shows two independently loading panels, a feed with a modal layered over the list, or a shell where the notification drawer must not re-render when the main content changes. Parallel routes let the root layout receive named slots alongside children, and each slot is its own page tree with its own loading and error states.
// app/layout.js â root layout with two extra slots
export default function RootLayout({ children, analytics, notifications }) {
return (
<html lang="en">
<body>
<div className="shell">
<main>{children}</main>
<aside>{notifications}</aside>
</div>
{analytics}
</body>
);
}
The slots are declared by placing matching folders in the tree: @analytics/page.js and @notifications/page.js fill those props. Because each slot is a separate tree, @notifications can keep its own state while children navigates â which is exactly how you build a modal that stays open while the page behind it changes, or a details pane that survives list navigation.
Slots also support conditional rendering through default.js, which decides what a slot shows when no page in the current segment provides a match. Without a default.js, navigation into a segment that lacks a matching slot throws a 404 for the whole page â the classic parallel-route trap.
Choosing between nesting, groups, and slots
Most teams reach for the heaviest tool first. A short decision sequence avoids that:
- If the chrome is shared by every route in a folder, use a nested
layout.js. - If two sections need different chrome but you do not want their folders visible in URLs, use a route group.
- If two regions must load, fail, or hold state independently at the same URL, use a parallel slot.
- If you only need the chrome to appear once on a single page, put it in the page â do not create a folder for it.
Steps one and two cover roughly nine out of ten real applications. Parallel routes earn their complexity on dashboards and media-heavy interfaces where independent lifecycles are the point.
Common Mistakes
Re-fetching shell data in every child page because the layout already loads it
Fix: Pass the layout's data down as props or leave it in the layout; duplicating the fetch doubles the latency for every navigation.
Creating a route group and expecting it to change the URL
Fix: Parentheses hide the folder from the path. To add a URL segment you need a real folder name.
Adding a parallel slot without a default.js
Fix: Provide default.js in the slot folder so segments without a matching page have something to render.
Wrapping the entire app in one layout because it is simpler
Fix: Split with route groups so auth, marketing and app sections each carry only the chrome they need.
Best Practices
- Keep layouts thin: render navigation and providers, defer per-page data to pages
- Place loading.js and error.js next to the segment that can actually fail
- Use route groups to give sections different shells without touching URLs
- Add default.js the moment you introduce a parallel slot
Interview Questions
Q1: What exactly persists when a user navigates between sibling routes?
The layout component instance persists: its DOM, state and effects survive because the router only replaces the children slot rather than remounting the tree.
Q2: How does a route group differ from a normal folder?
A folder in parentheses participates in the file tree and can hold a layout, but it contributes no segment to the URL, so grouping never changes what path a page serves.
Q3: Why does a parallel route need default.js?
Each slot is a separate page tree. When the active segment has no page for that slot, Next.js renders default.js instead of failing the entire navigation with a 404.
Key Takeaways
- Layouts wrap children and are not remounted on navigation, so their state and DOM persist for free.
- Nested folders nest layouts; loading and error files affect only their own segment.
- Route groups reorganise files and give sections different shells without adding URL segments.
- Parallel slots give one URL several independently loading, independently failing trees.
- Reach for the lightest tool: nested layout first, route group second, slots only when lifecycles must differ.