# Route interception

> Opening a route as a modal over the page you were on.

Route interception opens a route over the page you are on, usually as a modal.
Clicking a photo in a feed opens it in a modal, and the feed stays behind it.
Refreshing, or opening the link directly, shows the photo's own page:

```text
src/app/
  layout.tsx                  renders {children} and {modal}
  page.tsx                    the feed
  @modal/
    default.tsx               nothing, when no modal is open
    (.)photo/[id]/
      page.tsx                the photo, in a modal
  photo/[id]/
    page.tsx                  the photo's own page
```

The prefixes are the same as Next.js:

| Prefix | Intercepts |
| --- | --- |
| `(.)folder` | A route at the same level |
| `(..)folder` | A route one level up |
| `(...)folder` | A route from the app root |

## Opening a page in a modal

Interception builds on [parallel routes](/guides/routing#parallel-routes): the
modal is a slot. The layout receives it as a prop and renders it:

```tsx title="src/app/layout.tsx"
export default function Layout({
  children,
  modal,
}: {
  children: React.ReactNode;
  modal: React.ReactNode;
}) {
  return (
    <>
      <main>{children}</main>
      {modal}
    </>
  );
}
```

The slot renders nothing until a navigation puts something in it:

```tsx title="src/app/@modal/default.tsx"
export default function NoModal() {
  return null;
}
```

The interceptor is an ordinary page inside the slot. It receives the target
route's `params`, and it can be a server component. Only the frame around it
needs to be interactive:

```tsx title="src/app/@modal/(.)photo/[id]/page.tsx"
import { Suspense } from 'react';
import { findPhoto } from '@/data';

export default function PhotoModal({ params }: { params: Promise<{ id: string }> }) {
  return (
    <ModalShell>
      <Suspense fallback={<p>Loading…</p>}>
        <Photo params={params} />
      </Suspense>
    </ModalShell>
  );
}

async function Photo({ params }: { params: Promise<{ id: string }> }) {
  const { id } = await params;
  const photo = await findPhoto(id);

  return <img src={photo.url} alt={photo.title} />;
}
```

The photo's own page is what a refresh or a shared link renders:

```tsx title="src/app/photo/[id]/page.tsx"
import { findPhoto } from '@/data';

export default async function PhotoPage({ params }: { params: Promise<{ id: string }> }) {
  const { id } = await params;
  const photo = await findPhoto(id);

  return (
    <article>
      <h1>{photo.title}</h1>
      <img src={photo.url} alt={photo.title} />
    </article>
  );
}
```

Read the same data in both. An interceptor pointed at a different source than
the page it stands in for looks exactly like a broken interception: the modal
opens correctly and says the record does not exist.

## Closing the modal

The engine puts the interceptor in the slot. The ways to dismiss it are yours
to write, and a modal needs three: a close button, the `Escape` key, and a
click on the backdrop. The last two are the ones that get forgotten:

```tsx title="src/components/ModalShell.tsx"
"use client";

import { useEffect, useRef } from 'react';
import type { ReactNode } from 'react';

export function ModalShell({ children }: { children: ReactNode }) {
  const close = useRef<HTMLButtonElement>(null);

  useEffect(() => {
    // The only way to dismiss a dialog without a pointer.
    const onKey = (event: KeyboardEvent) => {
      if (event.key === 'Escape') history.back();
    };

    window.addEventListener('keydown', onKey);
    close.current?.focus();

    return () => window.removeEventListener('keydown', onKey);
  }, []);

  return (
    <div className="modal-backdrop" onClick={() => history.back()}>
      {/* A click inside the dialog is not a click on the backdrop. */}
      <article role="dialog" aria-modal="true" onClick={(e) => e.stopPropagation()}>
        <button ref={close} aria-label="Close" onClick={() => history.back()}>
          ×
        </button>
        {children}
      </article>
    </div>
  );
}
```

Close with `history.back()`, not a link to a fixed URL. Opening the modal
pushed a history entry over the page beneath, so going back returns to
whichever page it was opened over. A hardcoded destination is wrong as soon as
the same modal opens from two places.

The router sees going back as leaving the interception and empties the slot
without asking the server for anything. The page underneath never left, so
closing costs no request, and anything typed into it is still there.

A modal that opens from exactly one place can link to that place instead. A
real `<Link>` is worth keeping then: it opens in a new tab, and it works before
the page hydrates.

## Intercepting from another level

Use `(..)` for a route one level up, and `(...)` for a route from the app root:

```text
# From /feed, intercept /photo/[id] at the same level
src/app/@modal/(.)photo/[id]/page.tsx

# From /feed/trending, intercept /photo/[id] one level up
src/app/feed/@modal/(..)photo/[id]/page.tsx

# From /dashboard/settings, intercept /photo/[id] from the root
src/app/dashboard/settings/@drawer/(...)photo/[id]/page.tsx
```

## Intercepting into several slots

The same route can be intercepted into different slots:

```text
src/app/
  @modal/(.)photo/[id]/page.tsx       renders in the modal slot
  @preview/(.)photo/[id]/page.tsx     renders in the preview slot
  photo/[id]/page.tsx                 the photo's own page
```

## Seeing it work

The example app in the repository has one: `@modal/(.)posts/[slug]` over a
feed of posts. Click a post to open it in a modal, then refresh to get the
same route as a page.

## How it works

On a client navigation, through a `Link`:

- The client checks the intercept manifest, which the build generates.
- If the target URL matches an intercept pattern, headers are added to the
  request.
- The server resolves the current page from the referer URL, to know which
  slot the interceptor belongs in.
- It renders the interceptor *alone*, and says so with `X-RSC-Revalidate: modal`.
- The client puts it in that slot. The page underneath is never rendered
  again, so a half-filled form on it is still half-filled.

On a full page load, from a refresh or a direct URL, routes match as normal:
`/photo/123` renders `photo/[id]/page.tsx`, and nothing is intercepted.
