
- Next.js - Home
- Next.js - Overview
- Next.js - Project Setup
- Next.js - Folder Structure
- Next.js - App Router
- Next.js - Page Router
- Next.js Features
- Next.js - Pages
- Next.js - Data Fetching
- Next.js - ISR
- Next.js - Static File Serving
- Next.js - Pre-Rendering
- Next.js - Partial Pre Rendering
- Next.js - Server Side Rendering
- Next.js - Client Side Rendering
- Next.js Routing
- Next.js - Routing
- Next.js - Nested Routing
- Next.js - Dynamic Routing
- Next.js - Parallel Routing
- Next.js - Imperative Routing
- Next.js - Shallow Routing
- Next.js - Intercepting Routes
- Next.js - Redirecting Routes
- Next.js - Navigation and Linking
- Next.js Configuration
- Next.js - TypeScript
- Next.js - Environment Variables
- Next.js - File Conventions
- Next.js - ESLint
- Next.js API & Backend
- Next.js - API Routes
- Next.js - Dynamic API Routes
- Next.js - Route Handlers
- Next.js - API MiddleWares
- Next.js - Response Helpers
- Next.js API Reference
- Next.js - CLI Commands
- Next.js - Functions
- Next.js - Directives
- Next.js - Components
- Next.js - Image Component
- Next.js - Font Component
- Next.js - Head Component
- Next.js - Form Component
- Next.js - Link Component
- Next.js - Script Component
- Next.js Styling & SEO
- Next.js - CSS Support
- Next.js - Global CSS Support
- Next.js - Meta Data
- Next.js Advanced Topics
- Next.js - Error Handling
- Next.js - Server Actions
- Next.js - Fast Refresh
- Next.js - Internationalization
- Next.js - Authentication
- Next.js - Session Management
- Next.js - Authorization
- Next.js - Caching
- Next.js - Data Caching
- Next.js - Router Caching
- Next.js - Full Route Caching
- Next.js - Request Memoization
- Next.js Performance Optimization
- Next.js - Optimizations
- Next.js - Image Optimization
- Next.js - Lazy Loading
- Next.js - Font Optimization
- Next.js - Video Optimization
- Next.js - Script Optimization
- Next.js - Memory Optimization
- Next.js - Using OpenTelemetry
- Next.js - Package Bundling Optimization
- Next.js Testing
- Next.js - Testing
- Next.js - Testing with Jest
- Next.js - Testing with Cypress
- Next.js - Testing with Vitest
- Next.js - Testing with Playwright
- Next.js Debugging & Deployment
- Next.js - Debugging
- Next.js - Deployment
- Next.js Useful Resources
- Next.js - Interview Questions
- Next.js - Quick Guide
- Next.js - Useful Resources
- Next.js - Discussion
Next.js - Intercepting Routes
Intercepting Routes
The intercepting routes is Next.js 13 feature, that allows to load a route from another part of your application within the current layout. This is particularly useful when you want to create a overlay experience, without fully navigating away from the current page.
Example Use Case
Imagine, you need to make a photo gallery website in which, clicking a thumbnail shows a modal with the full photo while keeping the gallery visible in the background. This routes can be defined by,
app/ gallery/ page.js // Gallery with thumbnails [id]/ page.js // Full photo page (.)[id]/ page.js // Modal interceptor for photo
How Intercepting Routes Work
The working principle of intercepting routes can be explained as follows:
- Route Grouping: The intercepting routes are grouped together in a folder. This will tell Next.js that these routes should behave differently. The parent layout stays rendered even when you navigate to an intercepting route.
- Overlay Experience: When you navigate to an intercepting route, the parent layout will be rendered with a transparent background. The new UI will be overlaid on top of the parent layout.
-
Route naming convention: The intercepting routes use a special file naming convention with parentheses and dots,
- (.) - Intercept same segment level
- (..) - Intercept one segment level above
- (..)(..) - Intercept two segment levels above
- (...) - Intercept from the root app directory
Implementation and Example
To implement intercepting routes, First you need to create a base route. This route will be rendered with a transparent background. In the code below, we defined base route for the gallery website mentioned above.
// app/gallery/page.js export default function Gallery() { return ( <div> <h1>Photo Gallery</h1> <div className="grid"> {photos.map(photo => ( <Link key={photo.id} href={`/gallery/${photo.id}`}> <img src={photo.thumbnail} alt={photo.title} /> </Link> ))} </div> </div> ); } // app/gallery/[id]/page.js export default function PhotoPage({ params }) { const photo = getPhoto(params.id); return ( <div className="full-page"> <h1>{photo.title}</h1> <img src={photo.fullSize} alt={photo.title} /> <p>{photo.description}</p> </div> ); }
Output
This will create an experience where clicking a photo in the gallery shows it in a modal, but users can also share direct links to the full photo page. When each route activates,
- Direct navigation to /gallery/123 app/gallery/[id]/page.js
- Navigation from /gallery to /gallery/123 app/gallery/(.)[id]/page.js