Next.jsHard
What are intercepting routes?
Intercepting routes load a different route inside the current layout during client-side navigation, while a direct visit or refresh shows the route's normal page. The classic use is a photo feed where clicking a photo opens it in a modal but sharing the URL opens the full photo page.
Conventions
The folder prefix says where the intercepted route lives, relative to route segments (not the file system):
(.)photomatches a segment at the same level.(..)photomatches one level up.(..)(..)photomatches two levels up.(...)photomatches from the rootappdirectory.
Slots (@modal) and route groups do not count as segments when computing levels.
Modal pattern
- Create a parallel route slot
app/@modaland render{modal}in the root layout. - Add
app/@modal/(.)photos/[id]/page.tsxthat renders the photo inside a modal. - Keep the real page at
app/photos/[id]/page.tsx. - Add
app/@modal/default.tsxreturningnullso the slot is empty when no modal is open. - Close with
router.back().
What users get
- Clicking a link from the feed opens the modal, keeping the feed and its scroll position underneath.
- The URL changes to
/photos/123, so it can be shared. - Refreshing or opening the link directly shows the full page.
- Back closes the modal; Forward reopens it.
Pitfalls
- Interception only happens on soft navigation (
<Link>,router.push). - Missing
default.tsxin the slot breaks the build in Next.js 16. - To close the modal when navigating elsewhere, add a catch-all
@modal/[...catchAll]/page.tsxreturningnull.
Code example
tsx
1// app/layout.tsx2export default function RootLayout({ children, modal }: LayoutProps<"/">) {3 return (4 <html lang="en">5 <body>6 {children}7 {modal}8 </body>9 </html>10 );11}1213// app/@modal/default.tsx14// export default function Default() { return null; }1516// app/@modal/(.)photos/[id]/page.tsx - shown on soft navigation17import { Modal } from "@/components/modal"; // "use client", calls router.back() on close1819export default async function PhotoModal({ params }: PageProps<"/photos/[id]">) {20 const { id } = await params;21 return (22 <Modal>23 <img src={`/photos/${id}.jpg`} alt={`Photo ${id}`} />24 </Modal>25 );26}2728// app/photos/[id]/page.tsx - shown on direct visit or refresh29// export default async function PhotoPage({ params }) { ...full page... }
Practice this question
Open it in the study app to track progress, take notes, listen to the answer and work through the rest of the Next.js set.