
- 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 - Parallel Routing
Parallel Routing
Parallel Routing is a routing method, by which you can simultaneously and conditionally render one or more pages within the same layout. They are useful for dynamic sections of an app, such as dashboards and feeds on social sites.
Example Use Case
Imagine, you need to create a social media site, where the user can see their feed and their profile. The feed is displayed on the left side of the screen, and the profile is displayed on the right side of the screen. The following directory structure shows the parallel routing for the social media site.
app/ layout.tsx page.tsx @sidebar/ layout.tsx page.tsx @content/ layout.tsx page.tsx
How to Setup Parallel Routing
To setup parallel routing, follow the steps below:
Step 1: Enable App Router
Parallel routing is only supported in Next.js App Router. So make sure that your Next.js project is using Next.js 13 or later with the App Router enabled. See how to enable App Router.
Step 2: Create Named Slots
The parallel routes are created using named slots. Slots are defined with the @folder convention. For example, the following file structure defines two slots, @sidebar and @content.
app/ layout.tsx page.tsx @sidebar/ layout.tsx page.tsx @content/ layout.tsx page.tsx
Step 3: Define Layout File
Now, we need to define the root layout file. The root layout file is the layout that will be used for all the pages in the application. It is defined in the app/layout.tsx
file.
// File: app/layout.tsx export default function RootLayout({ children, sidebar, content, }: { children: React.ReactNode; sidebar: React.ReactNode; content: React.ReactNode; }) { return ( <html lang="en"> <body> <div style={{ display: "flex" }}> <aside style={{ width: "20%", background: "#f4f4f4" }}> {sidebar} {/* Sidebar slot */} </aside> <main style={{ width: "80%" }}> {content} {/* Main Content slot */} </main> </div> </body> </html> ); }
Step 4: Create Parallel Route Segments
After setting up the root layout, we can start creating individual parallel route segments. Each parallel route segment is defined in a folder with the @folder convention.
// File: @sidebar/page.tsx export default function Sidebar() { return <nav>Sidebar Content</nav>; } // File: @content/page.tsx export default function Content() { return <section>Main Content
</section>; }
Step 5: Define the Default Page
Finally, we need to define the default home page. This is the page that will be rendered when the user visits the root URL of the application.
// File: app/page.tsx export default function Home() { return <h1>Welcome to My Page</h1>; }
Example of Parallel Routing
In the example below, we will create a parallel routing for a social media site. The site will have a sidebar for displaying the user's feed and a main content area for displaying the user's profile. The sidebar will be displayed on the left side of the screen, and the main content area will be displayed on the right side of the screen.
// File: @sidebar/page.tsx export default function Sidebar() { return <nav>Sidebar Content</nav>; } // File: @content/page.tsx export default function Content() { return <section>Main Content
</section>; } // File: app/layout.tsx export default function RootLayout({ children, sidebar, content, }: { children: React.ReactNode; sidebar: React.ReactNode; content: React.ReactNode; }) { return ( <html lang="en"> <body> <div style={{ display: "flex" }}> <aside style={{ width: "20%", background: "#f4f4f4", padding: "10px" }}> {sidebar} {/* Sidebar slot */} </aside> <main style={{ width: "80%", fontWeight: "bold" }}> {content} {/* Main Content slot */} </main> </div> </body> </html> ); }
Output
In the output, we can see that the sidebar and main content are displayed side by side.
