Advanced Route Patterns

Loading concept...

πŸš€ Next.js Advanced Route Patterns

The Magic of Multiple Doors

Imagine your house has just one door. Everyone uses itβ€”guests, delivery people, family. Now imagine if your house could have multiple doors that work at the same time, and some doors could magically intercept visitors before they reach certain rooms!

That’s exactly what Next.js Advanced Route Patterns do for your web app. Let’s explore this magical house together!


🏠 Parallel Routes: Multiple Rooms, One View

What Are Parallel Routes?

Think of watching TV while checking your phone. Two things happening at the same time, in the same space.

Parallel Routes let you show multiple pages simultaneously in one layout. It’s like having a split-screen on your TV!

Real-Life Example

Picture a dashboard:

  • Left side: Your notifications
  • Right side: Your analytics
  • Both update independently!
app/
β”œβ”€β”€ layout.js
β”œβ”€β”€ @notifications/
β”‚   └── page.js
β”œβ”€β”€ @analytics/
β”‚   └── page.js
└── page.js

Why Is This Amazing?

Old Way With Parallel Routes
Load everything together Each section loads independently
One error breaks all Errors stay in their box
Hard to manage state Clean separation

🎰 Parallel Routes Slots: Named Boxes

What Are Slots?

Slots are like labeled boxes in your closet. Each box has a name starting with @.

@notifications  ← This is a slot!
@analytics      ← Another slot!
@sidebar        ← One more!

How Slots Work

graph TD A[Layout.js] --> B[@notifications] A --> C[@analytics] A --> D[Main Content] B --> E[Notifications Page] C --> F[Analytics Page]

Using Slots in Layout

// app/dashboard/layout.js

export default function Layout({
  children,
  notifications,
  analytics,
}) {
  return (
    <div className="dashboard">
      <main>{children}</main>
      <aside>{notifications}</aside>
      <section>{analytics}</section>
    </div>
  );
}

The Magic: Each slot is passed as a prop to your layout!


πŸ“„ default.js: The Backup Plan

Why Do We Need default.js?

Imagine you’re on a page, and one slot doesn’t have content for that route. What happens?

Without default.js: πŸ’₯ Error! With default.js: ✨ Shows fallback content!

The Problem It Solves

app/dashboard/
β”œβ”€β”€ layout.js
β”œβ”€β”€ @notifications/
β”‚   β”œβ”€β”€ page.js        ← Shows at /dashboard
β”‚   └── archive/
β”‚       └── page.js    ← Shows at /dashboard/archive
β”œβ”€β”€ @analytics/
β”‚   └── page.js        ← Only at /dashboard
└── page.js

When you go to /dashboard/archive:

  • @notifications/archive/page.js βœ… Exists
  • @analytics for /archive? ❌ Missing!

The Solution

// app/dashboard/@analytics/default.js

export default function AnalyticsDefault() {
  return (
    <div>
      <p>Select a view to see analytics</p>
    </div>
  );
}

Rule: Add default.js to every slot that might not match all routes!


🚦 Intercepting Routes: The Polite Doorman

What Are Intercepting Routes?

You’re walking to your bedroom. A polite doorman stops you:

β€œWait! I can show you a preview right here. Want the full experience? Keep walking!”

Intercepting routes catch a navigation and show something elseβ€”usually a modal or preview.

Real-World Example

Instagram-style photo viewing:

  1. You’re on the feed
  2. Click a photo β†’ Modal opens (intercepted!)
  3. Refresh page β†’ Full photo page loads

Both use /photo/123, but the experience is different!

graph TD A[User clicks photo] --> B{From feed?} B -->|Yes| C[Show Modal] B -->|No/Refresh| D[Full Photo Page] C --> E[Same URL: /photo/123] D --> E

πŸ“ Intercepting Route Conventions

The Special Symbols

Next.js uses special folder names to intercept:

Symbol Meaning Example
(.) Same level (.)photo
(..) One level up (..)photo
(..)(..) Two levels up (..)(..)photo
(...) From root (...)photo

How It Works

Think of it like file paths:

  • . means β€œright here”
  • .. means β€œgo up one folder”

Folder Structure Example

app/
β”œβ”€β”€ feed/
β”‚   β”œβ”€β”€ page.js
β”‚   └── (.)photo/
β”‚       └── [id]/
β”‚           └── page.js  ← Intercepts!
└── photo/
    └── [id]/
        └── page.js      ← Full page

When in /feed and you click /photo/123: β†’ (.)photo/[id]/page.js catches it!


πŸ–ΌοΈ Modal with Intercepting Routes

The Complete Recipe

Let’s build that Instagram-style modal!

Step 1: Create the Full Page

// app/photo/[id]/page.js

export default function PhotoPage({ params }) {
  return (
    <div className="full-photo">
      <h1>Photo {params.id}</h1>
      <img src={`/photos/${params.id}.jpg`} />
    </div>
  );
}

Step 2: Create the Interceptor

// app/feed/(.)photo/[id]/page.js

export default function PhotoModal({ params }) {
  return (
    <div className="modal-overlay">
      <div className="modal">
        <img src={`/photos/${params.id}.jpg`} />
      </div>
    </div>
  );
}

Step 3: Add Modal Slot to Layout

// app/feed/layout.js

export default function FeedLayout({
  children,
  modal,
}) {
  return (
    <>
      {children}
      {modal}
    </>
  );
}

The Complete Structure

app/
β”œβ”€β”€ feed/
β”‚   β”œβ”€β”€ layout.js
β”‚   β”œβ”€β”€ page.js
β”‚   └── @modal/
β”‚       β”œβ”€β”€ default.js      ← Empty when no modal
β”‚       └── (.)photo/
β”‚           └── [id]/
β”‚               └── page.js ← The modal!
└── photo/
    └── [id]/
        └── page.js         ← Full page

The default.js for Modal

// app/feed/@modal/default.js

export default function ModalDefault() {
  return null; // Nothing when modal is closed
}

🎯 Quick Reference

graph TD A[Advanced Patterns] --> B[Parallel Routes] A --> C[Intercepting Routes] B --> D[Slots @name] B --> E[default.js] C --> F[Conventions] C --> G[Modals] F --> H["#40;.#41; same level"] F --> I["#40;..#41; up one"] F --> J["#40;...#41; from root"]

πŸ’‘ When to Use What?

Pattern Use When
Parallel Routes Dashboard with independent sections
Slots Named regions that load separately
default.js A slot might not match the current URL
Intercepting Modals, previews, quick-views
Modal Pattern Photo galleries, settings panels

🌟 You Did It!

You now understand:

  • βœ… How to show multiple routes at once
  • βœ… How to name and use slots
  • βœ… Why default.js saves the day
  • βœ… How to intercept navigation
  • βœ… How to build beautiful modals

Your Next.js apps just got superpowers! πŸ¦Έβ€β™‚οΈ


Remember: Advanced patterns aren’t scaryβ€”they’re just tools that make complex UIs simple!

Loading story...

No Story Available

This concept doesn't have a story yet.

Story Preview

Story - Premium Content

Please sign in to view this concept and start learning.

Upgrade to Premium to unlock full access to all content.

Interactive Preview

Interactive - Premium Content

Please sign in to view this concept and start learning.

Upgrade to Premium to unlock full access to all content.

No Interactive Content

This concept doesn't have interactive content yet.

Cheatsheet Preview

Cheatsheet - Premium Content

Please sign in to view this concept and start learning.

Upgrade to Premium to unlock full access to all content.

No Cheatsheet Available

This concept doesn't have a cheatsheet yet.

Quiz Preview

Quiz - Premium Content

Please sign in to view this concept and start learning.

Upgrade to Premium to unlock full access to all content.

No Quiz Available

This concept doesn't have a quiz yet.