· By John Kavanagh

An App Router Dialog That Survives Refresh and Back

Photograph of a closed red theatre curtain lit from the stage.
Image by Rob Laughter, licensed from Unsplash.

An event opens neatly over the listing. The address bar changes, the background stays put, and closing the dialog returns me to where I started. Then I copy the URL into another tab and discover that the event apparently requires an audience before it can exist.

That is the part I want to get right first. The detail URL needs to work without the listing, without a remembered slot, and without a previous history entry belonging to this application.

My example is a small repair café listing. Opening an event from the list shows a dialog. Opening the same URL directly shows a complete event page. Refresh deliberately changes the presentation; it mustn't lose the event.


Start with the Page Someone Can Share

The ordinary route lives at app/events/[id]/page.tsx. It loads the named event, renders a heading and description, and includes a link back to all events. Its metadata uses the event's title and description too.

There is no dialog‑only loading ceremony to perform before this page becomes useful. Someone following a bookmark gets the content they asked for.

I keep the event lookup separate from its presentation. The full page and intercepted view use the same record and the same missing‑record decision. That avoids having one version call a cancelled workshop missing whilst the other confidently advertises it.

The full page uses an ordinary link to the listing, not a Close button wired to router.back(). A direct visitor's previous history entry could belong to another website. Sending them there would be an unnecessarily literal interpretation of closing the event.

For this fixture, events are public, fixed records. An application with permissions must make the same access decision for both representations; hiding the full page behind a dialog is not an access policy.


Add Interception Around That Existing Route

Next's version 13 interception guidance describes loading a route within the current context during client navigation. I use a root @dialog slot and put the intercepted event at app/@dialog/(.)events/[id]/page.tsx.

The (.) matcher refers to route segments. The @dialog folder is a slot, so it does not add another level to the URL. The public address remains /events/lamp.

The root layout receives the slot alongside children. Its relevant shape is small:

import type { ReactNode } from 'react';

export default function Layout({
  children,
  dialog,
}: {
  children: ReactNode;
  dialog: ReactNode;
}) {
  return (
    <html lang="en">
      <body>
        {children}
        {dialog}
      </body>
    </html>
  );
}

In the working example, a client provider inside body also records the opener and the deliberately removable listing item. It does not own whether the route is open. That belongs to the router.

A Next Link from the listing navigates to the detail URL. The intercepted view supplies the dialog, whilst the listing stays mounted beneath it. A temporary note in the listing makes that preservation visible. It is only component state: a hard reload starts a new page and a fresh note.

The full detail page remains the direct representation of the same URL. I don't duplicate the entire page inside the overlay or put the only useful content in a component which renders nothing until a client effect runs.


Give an Empty Slot Two Different Jobs

The awkward case is leaving the overlay for an unrelated route under the same root layout. In my example, the dialog contains a link to the café's About page.

The main content can successfully change to About whilst the dialog still shows the previous event. The URL and the visible overlay now disagree. This is easy to miss if the only dismissal path in the demonstration is the Close button.

A default.tsx returning null handles a missing slot state during hard entry. It does not promise to clear a previously active slot on every soft navigation. The parallel‑route documentation makes that distinction explicit.

I provide an explicit empty slot route for About as well:

// app/@dialog/about/page.tsx
export default function ClearDialog() {
  return null;
}

There is also a null app/@dialog/default.tsx, and a null slot index for the root URL. They may return the same small value, but they are not interchangeable files. In this Next 13 fixture, a catch‑all alone leaves the previous dialog open on the About transition; the named route clears it. I test that destination rather than assuming a general fallback covers it.

I keep a default‑only variant of the application so that I can test the difference. Navigating from its open dialog to About leaves the old overlay present; loading About directly recovers an empty slot. Removing the default as well creates a separate hard‑entry failure. Testing those controls is more useful than deciding the complete example looks reassuring.


Let History Dismiss the Contextual View

The intercepted dialog exists because this application added a client‑navigation entry. Its Close button and Escape handler therefore ask the router to go Back.

I avoid adding a second isOpen flag which can disagree with the URL. Hiding the dialog locally would leave the detail address selected, and a subsequent Forward or Back operation would have to reconcile two different accounts of what is open.

The acceptance sequence opens an event, goes Back, then goes Forward. Back returns to the listing; Forward restores the contextual event. Refresh at the event URL renders the full page instead. These are different expectations, and an assertion which merely finds the event title cannot distinguish them.

Navigation to About selects a new route and the explicit null slot removes the overlay. It should not first go Back and then push About, unnecessarily rewriting the user's history to make the animation convenient.


Keep the Dialog's Keyboard Contract

Routing does not supply the rest of a modal's behaviour. My dialog has an accessible name, moves initial focus to its heading, keeps Tab and Shift+Tab within its controls, and restores focus when it closes.

The native dialog element's showModal() places it in the top layer and makes the background inert. The wrapper also restores the body's previous scroll setting on unmount. Escape requests route dismissal rather than closing the element behind the router's back.

My article on React portals covers rendering outside the usual DOM parent. This wrapper uses that arrangement after mounting, but the portal itself does not make an element modal. The native modal call and the interaction rules do that work.

I record the actual link which opened the event. If it still exists, closing returns focus there. If it has disappeared, the fallback is the listing heading. The fixture includes a button which removes the lamp link whilst its dialog is open, so the less convenient case is exercised too.

The browser checks inspect focus and attempt to focus the background input. They do not establish what a screen reader announces. As in my accessibility pitfalls article, the interaction needs checking beyond a successful mouse click. Speech output, zoom, and the intended browser combinations still deserve their own review.


Test the Two Representations Together

I run the navigation sequence against a production build, including a direct detail request, refresh, Back and Forward, unrelated navigation, and a missing opener. The tests assert whether a dialog or full‑page heading is present, not just whether the URL changed.

Missing and throwing event lookups have separate recovery paths. I inspect their HTTP responses as well as their visible messages; a page which says an event is missing is not, by itself, evidence of a 404. The direct page also needs meaningful metadata when no listing has been visited.

I would keep this example small until those cases are settled. Animating the overlay is easy to add afterwards. A copied URL which only works if you approach it correctly is harder to explain to the person who copied it.

Postscript

November 2026: My route‑dialog lab now compares Next.js 13.5.6 on Node 20.6.1 with Next.js 16.4.6 on Node 22.23.5. The current build rejects the missing‑default control through strict route matching; the historical fixture renders its unmatched‑slot error UI after an HTTP 200 response. Both retain the default‑only stale‑slot control. I've adapted the keyboard behaviour from my React overlay fixture, with the full navigation matrix alongside it. Chrome 157 checks focus and route outcomes; the included manual tasks remain separate from any claim about screen‑reader speech.

Downloadable Resource

  • Next.js Route Dialog Navigation Lab

    Test a route‑backed dialog through refresh, Back, Forward, direct entry, and unrelated navigation. Includes slot controls, keyboard focus, and recovery checks.

    Download ZIP

Need a senior engineer involved?

I can work directly in the codebase, review the architecture, or support the team through delivery when the work needs more than extra hands.