Menu

Intercepting Routes and Modals

Lesson 2: Intercepting Routes and Modals

What is an intercepting route?

An intercepting route shows a different UI for a URL when you navigate to it from inside the app. A direct visit or refresh shows the normal page.

Classic example: clicking a course card on /courses opens a preview modal at /courses/sql. Sharing that URL or refreshing shows the full course page.

Convention Intercepts a segment…
(.)folder At the same level
(..)folder One level up
(..)(..)folder Two levels up
(...)folder From the root app folder

Levels are counted by route segments, not file-system folders, so route groups and slots are ignored.

Example

app/courses/
├─ layout.tsx
├─ page.tsx                     # /courses (grid of cards)
├─ [slug]/page.tsx              # /courses/sql (full page)
└─ @modal/
   ├─ default.tsx               # returns null
   └─ (.)[slug]/page.tsx        # Modal version of /courses/sql
// app/courses/layout.tsx
export default function Layout({ children, modal }: { children: React.ReactNode; modal: React.ReactNode }) {
  return (
    <>
      {children}
      {modal}
    </>
  );
}
// app/courses/@modal/(.)[slug]/page.tsx
import { Modal } from '@/components/Modal';

export default async function CoursePreview({ params }: PageProps<'/courses/[slug]'>) {
  const { slug } = await params;
  const course = await getCourse(slug);
  return (
    <Modal>
      <h2>{course.title}</h2>
      <p>{course.summary}</p>
    </Modal>
  );
}
// components/Modal.tsx
'use client';
import { useRouter } from 'next/navigation';

export function Modal({ children }: { children: React.ReactNode }) {
  const router = useRouter();
  return (
    <div className="fixed inset-0 bg-black/50" onClick={() => router.back()}>
      <div role="dialog" aria-modal="true" className="m-auto mt-24 max-w-lg rounded bg-white p-6" onClick={(e) => e.stopPropagation()}>
        {children}
      </div>
    </div>
  );
}

How it works

  1. On /courses, the @modal slot renders default.tsx (nothing).
  2. Clicking <Link href="/courses/sql"> is intercepted by (.)[slug], so the modal renders in the slot while the grid stays visible.
  3. The URL changes to /courses/sql, so the modal can be shared or bookmarked.
  4. A refresh or direct visit skips interception and renders [slug]/page.tsx in full.
  5. router.back() closes the modal and returns to the grid.

Common mistakes

Mistake Effect Fix
Counting folders instead of segments for (..) Interception never happens Ignore route groups and @slots when counting
No default.tsx in @modal Build error or 404 Return null from default.tsx
Modal without keyboard or focus handling Inaccessible Close on Escape, trap focus, use role="dialog"

Key takeaways

  • Intercepting + parallel routes = modals with real, shareable URLs.
  • Navigation shows the modal; refresh shows the full page.

Try it: Add a course preview modal to the LearnHub course grid that closes with the Escape key.


Nextjs Tutorial

J

Jebasta





Get in Touch For Details