Skip to content

Lesson 8 of 27Routing in depth

Parallel and Intercepting Routes

Build dashboards with independent panels and modals with real URLs using slots, default.tsx, interception markers and route groups.

18 min read0/4 questions

The product team wants the issue tracker to work like Linear. Click an issue in the list and it opens in a panel over the list, with the URL changing to /issues/ENG-142. Paste that link into Slack and it opens as a full page. Refresh keeps you on the issue, and the back button closes the panel. With plain React state you get maybe two of those four. Next.js gives you all four with two folder conventions: parallel routes (@slot folders that render several pages at once) and intercepting routes ((.) folders that show another route inside the current layout). Layouts and Navigation showed you the folder names. This lesson explains how the router actually resolves them, so you can build the patterns and fix them when they break.

Slots: several pages in one layout

A folder whose name starts with @ is a slot. Its contents are rendered by the parent layout, passed in as a prop with the same name as the slot. Slots are not URL segments: app/dashboard/@revenue/page.tsx is part of /dashboard, not /dashboard/revenue.

A dashboard with three independent panels
  • app/dashboard/
  • layout.tsxreceives children, revenue, alerts
  • page.tsxchildren: the main panel
  • default.tsxfallback for children on hard navigation
  • @revenue/
  • page.tsxslow query against the warehouse
  • loading.tsxskeleton for this panel only
  • error.tsxerror UI for this panel only
  • default.tsx
  • @alerts/
  • page.tsx
  • default.tsx
app/dashboard/layout.tsx
export default function DashboardLayout(props: LayoutProps<"/dashboard">) {
  return (
    <div className="grid grid-cols-3 gap-4">
      <main className="col-span-2">{props.children}</main>
      <aside>{props.revenue}</aside>
      <aside>{props.alerts}</aside>
    </div>
  );
}

LayoutProps<"/dashboard"> is generated from the folders, so revenue and alerts are typed for you, and renaming a slot folder breaks the build where it should. children is just an implicit slot: app/dashboard/page.tsx behaves as if it were app/dashboard/@children/page.tsx.

Why not just render three components in page.tsx? Because each slot is a full route segment with its own special files. @revenue/loading.tsx becomes a Suspense boundary around that panel only, so a slow warehouse query does not hold back alerts. @revenue/error.tsx becomes an error boundary around that panel only, so a failed query shows "Revenue unavailable, retry" in one box instead of replacing the dashboard. Each slot can also navigate on its own, which is the part that needs a mental model.

How a slot decides what to render

On every navigation, the router matches the URL against each slot separately. Say @alerts has a page at @alerts/settings/page.tsx but @revenue does not, and the user goes from /dashboard to /dashboard/settings. @alerts matches. @revenue has nothing for settings. What it shows depends on the kind of navigation:

  • Soft navigation (a <Link> click or router.push): the router keeps each slot's active state. @revenue keeps showing what it showed before, even though it does not match the new URL.
  • Hard navigation (first load, refresh, opening the link in a new tab): there is no previous state to keep. The server renders the slot's default.tsx instead.
One URL, two kinds of navigation
BrowserRouter@alerts slot@revenue slotLink click to /dashboard/settingsrender settings/page.tsxkeep previous pagerefresh on /dashboard/settingsrender default.tsx

A soft navigation. The router fetches only the segments that changed.

1/5

default.tsx is required

In Next.js 16, every named slot needs a default.tsx, or the build fails. You choose what it means:

  • return null: the slot is simply empty when it does not match. Right for modals and optional panels.
  • notFound(): an unmatched slot makes the whole URL a 404. This was the implicit behaviour before 16, and it is right when the page makes no sense without that panel.
  • The same content as page.tsx: often the simplest choice for a dashboard panel that should always show.

children needs one too when you have slots with sub-pages, since it is a slot. Without app/dashboard/default.tsx, a hard navigation to a URL only some slot matches renders a 404 for the whole route. "Works when I click, 404 when I refresh" is almost always this.

Quick check
app/@modal/(.)photos/[id]/page.tsx exists, but app/@modal/ has no default.tsx. What happens in Next.js 16?
Need a nudge? A hint rules out a wrong answer.

Tabs inside a slot and reading the active segment

A slot can have its own layout.tsx, which turns it into an independently navigable area. Put a tab bar in @revenue/layout.tsx with links to /dashboard/by-region and /dashboard/by-plan, and only that panel changes when you switch tabs. To highlight the active tab from the parent, a Client Component can call useSelectedLayoutSegment("revenue"), which returns the active segment inside that slot.

Conditional slots, and the security trap

Slots make role-based screens tidy. One layout, two slots, pick one:

app/dashboard/layout.tsx
import { getSession } from "@/lib/dal";
 
export default async function Layout(props: LayoutProps<"/dashboard">) {
  const session = await getSession();
  return session?.role === "admin" ? props.admin : props.member;
}

Here is what catches people out: both slots render on the server, whichever one the layout returns. The @admin/page.tsx runs its queries for every member, and its output is part of the response. The condition controls what is shown, not what runs. So authorize inside the admin slot's data calls too (in your data access layer, which is the right place anyway), and never treat "the layout did not return it" as a permission check.

Intercepting routes

An intercepting route says: "when the user navigates from here to that other URL, render my version instead." The folder name starts with a marker that works like a relative path, but counts route segments:

MarkerMatches a route
(.)photosphotos at the same level
(..)photosphotos one segment up
(..)(..)photosphotos two segments up
(...)photosphotos from the root of app

The key word is segments. Slot folders (@modal) and route groups ((shop)) are not segments, so they do not count when you add dots. app/feed/@modal/(..)photos intercepts /photos: one level up from /feed, even though it is two folders up on disk. Getting the count wrong is the most common reason "the modal never opens": the intercept points at a route that does not exist, and the link just goes to the full page.

Interception only happens on soft navigation. A refresh, a pasted URL, a new tab or a plain <a> tag is a hard navigation, and the real route renders. That is the feature: the same URL is a modal in context and a full page on its own.

This resolver follows the same rules. Run it and try your own folder paths:

TypeScriptintercept-target.ts
const MARKER = /^(\(\.\.\.\)|(?:\(\.\.\))+|\(\.\))(.+)$/; function interceptTarget(folder: string): string { const parts = folder.split("/").slice(1); // drop "app" const base: string[] = []; for (let i = 0; i < parts.length; i++) { const part = parts[i]; const m = part.match(MARKER); if (!m) { const isSlot = part.startsWith("@"); const isGroup = part.startsWith("(") && part.endsWith(")"); if (!isSlot && !isGroup) base.push(part); // only real segments count continue; } const marker = m[1]; let kept = base; if (marker === "(...)") kept = []; else if (marker !== "(.)") kept = base.slice(0, base.length - marker.length / 4); return "/" + [...kept, m[2], ...parts.slice(i + 1)].join("/"); } return "no intercept marker";} console.log(interceptTarget("app/@modal/(.)photos/[id]"));console.log(interceptTarget("app/feed/@modal/(..)photos/[id]"));console.log(interceptTarget("app/(shop)/products/[slug]/(..)(..)cart"));console.log(interceptTarget("app/settings/billing/(...)login"));
Output
Press Run (⌘ Enter) to see what happens.
Predict the outputTypeScript

What does this print?

// Same interceptTarget() as above
console.log(interceptTarget("app/teams/[team]/@panel/(..)members/[id]"));
Need a nudge? Hints walk you through it.

The modal pattern, end to end

Put the two features together and you get a modal with a real URL. Here is the photo version; the issue panel from the intro is identical with different names.

Photo modal: parallel + intercepting
  • app/
  • layout.tsxrenders {children} and {modal}
  • page.tsxthe photo grid at /
  • photos/[id]/
  • page.tsxfull page for hard navigation
  • @modal/
  • default.tsxnull: no modal
  • [...catchAll]/
  • page.tsxnull: closes the modal on other routes
  • (.)photos/[id]/
  • page.tsxsame photo, inside <Modal>
  • _components/
  • PhotoView.tsxshared Server Component
  • Modal.tsx"use client" shell
app/layout.tsx
export default function RootLayout(props: LayoutProps<"/">) {
  return (
    <html lang="en">
      <body>
        {props.children}
        {props.modal}
      </body>
    </html>
  );
}
app/@modal/(.)photos/[id]/page.tsx
import { Modal } from "@/app/_components/Modal";
import { PhotoView } from "@/app/_components/PhotoView";
 
export default async function PhotoModal({ params }: { params: Promise<{ id: string }> }) {
  const { id } = await params;
  return (
    <Modal>
      <PhotoView id={id} />
    </Modal>
  );
}
app/_components/Modal.tsx
"use client";
 
import { useEffect, useRef } from "react";
import { useRouter } from "next/navigation";
 
export function Modal({ children }: { children: React.ReactNode }) {
  const router = useRouter();
  const ref = useRef<HTMLDialogElement>(null);
 
  useEffect(() => {
    if (!ref.current?.open) ref.current?.showModal(); // focus trap and Escape for free
  }, []);
 
  return (
    <dialog ref={ref} onClose={() => router.back()} aria-label="Photo">
      {children}
      <button onClick={() => router.back()}>Close</button>
    </dialog>
  );
}

PhotoView is a Server Component used by both the modal page and the full page, so the data fetching lives in one place and the modal ships no extra client code. The Modal shell is the only client part, and passing the server content in as children keeps it that way (the composition pattern from Server and Client Components). A native <dialog> opened with showModal() gives you focus trapping, Escape to close and an inert background.

Put it in order4 steps

A user on the grid clicks photo 42, then refreshes, then presses back. Put what they see in order.

  1. Tap the steps below in the right order.
Need a nudge? A hint shows where it starts.

Closing the modal, and the bugs you will meet

Closing with router.back() is the usual choice. It pops the history entry the click created, so the URL, the back button and the modal stay in sync.

Closing by navigating elsewhere is where people get stuck. Remember the active state rule: on a soft navigation, a slot that does not match the new URL keeps showing what it had. So clicking a <Link href="/about"> from inside the modal changes the page underneath, and the modal stays open on top. The fix is to give the slot something to match that renders nothing: @modal/[...catchAll]/page.tsx returning null for every other route (plus @modal/page.tsx returning null if you link back to /).

The other usual suspects:

  • 404 or build failure on refresh: a missing default.tsx for @modal or for children.
  • The modal never opens: wrong number of dots (count segments, not folders), or the link is a plain <a> or window.location change, which is a hard navigation.
  • Focus and scroll problems: build on <dialog>, and return focus to the clicked item if your design system does not do it for you.

Route groups and multiple root layouts

Route groups, (name) folders, were introduced in App Router Routing as a way to organize without changing URLs. They have one more power that matters in large apps: multiple root layouts. Remove app/layout.tsx and give each group its own layout with <html> and <body>:

app/
  (marketing)/layout.tsx    # html, body, marketing nav, analytics script
  (marketing)/page.tsx      # /
  (marketing)/pricing/page.tsx
  (app)/layout.tsx          # html, body, auth providers, sidebar
  (app)/dashboard/page.tsx  # /dashboard

The marketing site and the product now share nothing: different fonts, providers and scripts, so the public pages do not ship the product's client bundle. Three rules come with it. Navigating between groups with different root layouts is a full page load, not a soft navigation. Two groups must not resolve to the same URL ((marketing)/about and (app)/about is an error). And without a top-level layout, the home page / must live inside one of the groups.

Match the pairs0/5 matched

Match each requirement to the convention.

Choose an item, then its match on the other side.
Key terms
slot
An @name folder whose page is passed to the parent layout as a prop. Not part of the URL.
default.tsx
What a slot renders when it does not match the URL on a hard navigation. Required for every slot in Next.js 16.
soft navigation
A client-side navigation where the router keeps each slot's active state and fetches only changed segments.
hard navigation
A full page load (first visit, refresh, new tab) where slots that do not match render their default.tsx.
intercepting route
A folder prefixed with (.), (..) or (...) that renders another route inside the current layout on soft navigation.
active state
The page a slot is currently showing, which the router keeps during soft navigation even when the URL no longer matches it.
multiple root layouts
Route groups that each define their own <html> layout; moving between them causes a full page load.