Skip to content
nextjs-routing-advanced logo

Next.js Advanced Routing

nextjs-routing-advanced

Complete Next.js advanced routing system. PROACTIVELY activate for: (1) Dynamic routes with [slug], (2) Catch-all routes [...slug], (3) Route groups for organization, (4) Parallel routes with @slot, (5) Intercepting routes for modals, (6) Private folders with _prefix, (7) Route Handlers (API), (8...

SKILL.md

Full skill instructions

Quick Reference

Route TypeFolder PatternURL Example
Dynamic[slug]/​blog/​hello → { slug: 'hello' }
Catch-all[...slug]/​docs/​a/​b → { slug: ['a', 'b'] }
Optional catch-all[[...slug]]/​shop or /​shop/​a/​b
Route group(name)No URL impact, layout grouping
Parallel route@slotIndependent loading/​error
Intercept same level(.)pathModal pattern
Private folder_folderNot a route
NavigationCodeUse Case
Link<Link href="/​path">Declarative nav
router.pushrouter.push('/​path')Programmatic nav
router.replacerouter.replace('/​path')No history entry
redirectredirect('/​path')Server redirect
Route HandlerMethodPattern
GETReadexport async function GET() {}
POSTCreateexport async function POST() {}
PUTUpdateexport async function PUT() {}
DELETEDeleteexport async function DELETE() {}

When to Use This Skill

Use for advanced routing patterns:

  • Dynamic blog/​product pages with slugs
  • Documentation with catch-all routes
  • Dashboard layouts with parallel routes
  • Photo gallery modals with intercepting routes
  • API endpoints with Route Handlers

Related skills:

  • For App Router basics: see nextjs-app-router
  • For middleware routing: see nextjs-middleware
  • For data in routes: see nextjs-data-fetching

Next.js Advanced Routing

Dynamic Routes

Single Dynamic Segment

// app/​blog/[slug]/​page.tsx
interface PageProps {
  params: Promise<{ slug: string }>;
}

export default async function BlogPost({ params }: PageProps) {
  const { slug } = await params;
  const post = await getPost(slug);

  return <article>{post.content}</​article>;
}

// /​blog/​hello-world → { slug: 'hello-world' }

Multiple Dynamic Segments

// app/​shop/[category]/[product]/​page.tsx
interface PageProps {
  params: Promise<{ category: string; product: string }>;
}

export default async function ProductPage({ params }: PageProps) {
  const { category, product } = await params;
  const productData = await getProduct(category, product);

  return <div>{productData.name}</​div>;
}

// /​shop/​electronics/​laptop → { category: 'electronics', product: 'laptop' }

Catch-All Segments

// app/​docs/[...slug]/​page.tsx
interface PageProps {
  params: Promise<{ slug: string[] }>;
}

export default async function DocsPage({ params }: PageProps) {
  const { slug } = await params;
  // slug is an array of path segments
  const doc = await getDoc(slug.join('/'));

  return <div>{doc.content}</​div>;
}

// /​docs/​getting-started → { slug: ['getting-started'] }
// /​docs/​api/​auth/​login → { slug: ['api', 'auth', 'login'] }

Optional Catch-All Segments

// app/​shop/[[...slug]]/​page.tsx
interface PageProps {
  params: Promise<{ slug?: string[] }>;
}

export default async function ShopPage({ params }: PageProps) {
  const { slug } = await params;

  if (!slug) {
    // /​shop - show all products
    return <AllProducts />;
  }

  if (slug.length === 1) {
    // /​shop/​category - show category
    return <CategoryProducts category={slug[0]} />;
  }

  // /​shop/​category/​product - show product
  return <ProductDetail category={slug[0]} product={slug[1]} />;
}

// /​shop → { slug: undefined }
// /​shop/​electronics → { slug: ['electronics'] }
// /​shop/​electronics/​laptop → { slug: ['electronics', 'laptop'] }

Route Groups

Organizing Without URL Impact

app/
├── (marketing)/
│   ├── layout.tsx      # Marketing layout
│   ├── about/
│   │   └── page.tsx    # /​about
│   └── contact/
│       └── page.tsx    # /​contact
│
├── (shop)/
│   ├── layout.tsx      # Shop layout
│   ├── products/
│   │   └── page.tsx    # /​products
│   └── cart/
│       └── page.tsx    # /​cart
│
└── (auth)/
    ├── layout.tsx      # Auth layout (centered, minimal)
    ├── login/
    │   └── page.tsx    # /​login
    └── register/
        └── page.tsx    # /​register

Multiple Root Layouts

// app/(marketing)/​layout.tsx
export default function MarketingLayout({
  children,
}: {
  children: React.ReactNode;
}) {
  return (
    <html>
      <body>
        <MarketingHeader />
        {children}
        <MarketingFooter />
      </​body>
    </​html>
  );
}

// app/(app)/​layout.tsx
export default function AppLayout({
  children,
}: {
  children: React.ReactNode;
}) {
  return (
    <html>
      <body>
        <AppSidebar />
        <main>{children}</​main>
      </​body>
    </​html>
  );
}

Parallel Routes

Basic Parallel Routes

app/
├── layout.tsx
├── page.tsx
├── @analytics/
│   ├── page.tsx
│   └── loading.tsx
├── @team/
│   ├── page.tsx
│   └── loading.tsx
└── @notifications/
    └── page.tsx
// app/​layout.tsx
export default function Layout({
  children,
  analytics,
  team,
  notifications,
}: {
  children: React.ReactNode;
  analytics: React.ReactNode;
  team: React.ReactNode;
  notifications: React.ReactNode;
}) {
  return (
    <div className="dashboard">
      <main>{children}</​main>
      <aside>
        {analytics}
        {team}
        {notifications}
      </​aside>
    </​div>
  );
}

Conditional Rendering with Parallel Routes

// app/​layout.tsx
import { auth } from '@/​lib/​auth';

export default async function Layout({
  children,
  admin,
  user,
}: {
  children: React.ReactNode;
  admin: React.ReactNode;
  user: React.ReactNode;
}) {
  const session = await auth();

  return (
    <div>
      {children}
      {session?.role === 'admin' ? admin : user}
    </​div>
  );
}

Default Slots

// app/​@analytics/​default.tsx
// Shown when the slot doesn't match current route
export default function AnalyticsDefault() {
  return null; // or a default UI
}

Intercepting Routes

Modal Pattern

app/
├── feed/
│   └── page.tsx              # /​feed - main feed
├── photo/
│   └── [id]/
│       └── page.tsx          # /​photo/​123 - full page photo
└── @modal/
    └── (.)photo/
        └── [id]/
            └── page.tsx      # Intercepted: shows modal

Intercepting Conventions

(.)  - Match same level
(..) - Match one level above
(..)(..) - Match two levels above
(...) - Match from root

Photo Gallery Modal Example

// app/​feed/​page.tsx
import Link from 'next/​link';

export default function FeedPage() {
  const photos = await getPhotos();

  return (
    <div className="grid">
      {photos.map((photo) => (
        <Link key={photo.id} href={`/​photo/​${photo.id}`}>
          <img src={photo.thumbnail} alt={photo.title} />
        </​Link>
      ))}
    </​div>
  );
}
// app/​@modal/(.)photo/[id]/​page.tsx
import { Modal } from '@/​components/​modal';

export default async function PhotoModal({
  params,
}: {
  params: Promise<{ id: string }>;
}) {
  const { id } = await params;
  const photo = await getPhoto(id);

  return (
    <Modal>
      <img src={photo.url} alt={photo.title} />
      <p>{photo.description}</​p>
    </​Modal>
  );
}
// app/​photo/[id]/​page.tsx - Full page view (direct navigation)
export default async function PhotoPage({
  params,
}: {
  params: Promise<{ id: string }>;
}) {
  const { id } = await params;
  const photo = await getPhoto(id);

  return (
    <div className="photo-page">
      <img src={photo.url} alt={photo.title} />
      <h1>{photo.title}</​h1>
      <p>{photo.description}</​p>
    </​div>
  );
}
// app/​layout.tsx
export default function RootLayout({
  children,
  modal,
}: {
  children: React.ReactNode;
  modal: React.ReactNode;
}) {
  return (
    <html>
      <body>
        {children}
        {modal}
      </​body>
    </​html>
  );
}
// components/​modal.tsx
'use client';

import { useRouter } from 'next/​navigation';

export function Modal({ children }: { children: React.ReactNode }) {
  const router = useRouter();

  return (
    <div className="modal-overlay" onClick={() => router.back()}>
      <div className="modal-content" onClick={(e) => e.stopPropagation()}>
        <button onClick={() => router.back()}>Close</​button>
        {children}
      </​div>
    </​div>
  );
}

Private Folders

app/
├── _components/         # Private - not a route
│   ├── Button.tsx
│   └── Card.tsx
├── _lib/               # Private - not a route
│   └── utils.ts
├── dashboard/
│   ├── _components/    # Private - scoped to dashboard
│   │   └── Chart.tsx
│   └── page.tsx
└── page.tsx

Route Handlers

HTTP Methods

// app/​api/​posts/​route.ts
import { NextResponse } from 'next/​server';

export async function GET(request: Request) {
  const posts = await getPosts();
  return NextResponse.json(posts);
}

export async function POST(request: Request) {
  const body = await request.json();
  const post = await createPost(body);
  return NextResponse.json(post, { status: 201 });
}

export async function PUT(request: Request) {
  const body = await request.json();
  const post = await updatePost(body);
  return NextResponse.json(post);
}

export async function DELETE(request: Request) {
  await deletePost();
  return new NextResponse(null, { status: 204 });
}

Dynamic Route Handlers

// app/​api/​posts/[id]/​route.ts
interface RouteContext {
  params: Promise<{ id: string }>;
}

export async function GET(request: Request, context: RouteContext) {
  const { id } = await context.params;
  const post = await getPost(id);

  if (!post) {
    return NextResponse.json({ error: 'Not found' }, { status: 404 });
  }

  return NextResponse.json(post);
}

Route Handler Options

// Force dynamic
export const dynamic = 'force-dynamic';

// Set runtime
export const runtime = 'edge';

// Set revalidation
export const revalidate = 60;

URL Query Parameters

Accessing Search Params

// app/​search/​page.tsx
interface SearchPageProps {
  searchParams: Promise<{ q?: string; page?: string; sort?: string }>;
}

export default async function SearchPage({ searchParams }: SearchPageProps) {
  const { q, page = '1', sort = 'relevance' } = await searchParams;

  const results = await search({
    query: q,
    page: parseInt(page),
    sort,
  });

  return (
    <div>
      <h1>Results for: {q}</​h1>
      <SearchResults results={results} />
    </​div>
  );
}

Client-Side URL Updates

'use client';

import { useRouter, useSearchParams, usePathname } from 'next/​navigation';

export function SearchFilter() {
  const router = useRouter();
  const pathname = usePathname();
  const searchParams = useSearchParams();

  const updateSearch = (key: string, value: string) => {
    const params = new URLSearchParams(searchParams.toString());
    params.set(key, value);
    router.push(`${pathname}?${params.toString()}`);
  };

  return (
    <select onChange={(e) => updateSearch('sort', e.target.value)}>
      <option value="relevance">Relevance</​option>
      <option value="date">Date</​option>
      <option value="price">Price</​option>
    </​select>
  );
}

Programmatic Navigation

useRouter Hook

'use client';

import { useRouter } from 'next/​navigation';

export function NavigationExample() {
  const router = useRouter();

  return (
    <div>
      <button onClick={() => router.push('/​dashboard')}>
        Go to Dashboard
      </​button>
      <button onClick={() => router.replace('/​login')}>
        Replace with Login
      </​button>
      <button onClick={() => router.back()}>
        Go Back
      </​button>
      <button onClick={() => router.forward()}>
        Go Forward
      </​button>
      <button onClick={() => router.refresh()}>
        Refresh
      </​button>
      <button onClick={() => router.prefetch('/​about')}>
        Prefetch About
      </​button>
    </​div>
  );
}

redirect() Function

// In Server Component or Server Action
import { redirect } from 'next/​navigation';

export default async function ProtectedPage() {
  const session = await getSession();

  if (!session) {
    redirect('/​login');
  }

  return <div>Protected content</​div>;
}

permanentRedirect() Function

import { permanentRedirect } from 'next/​navigation';

export default async function OldPage() {
  permanentRedirect('/​new-page'); // 308 status
}

Best Practices

PracticeDescription
Use route groups for organizationGroup by feature or layout
Implement loading statesAdd loading.tsx for each segment
Use parallel routes for dashboardsIndependent loading/​error states
Intercept for modalsBetter UX for overlays
Keep private folders organizedUse _ prefix for non-routes
Type your paramsUse Promise<> for params and searchParams

More skills from JosiahSiegel

docker-git-bash-guide logo
JosiahSiegel/claude-plugin-marketplace

docker-git-bash-guide

Docker on Windows with Git Bash / MINGW path conversion. PROACTIVELY activate for: (1) Docker commands failing on Windows Git Bash with path errors, (2) volume mount path conversion (-v $(pwd):/app), (3) MSYS_NO_PATHCONV and MSYS2_ARG_CONV_EXCL workarounds, (4) double-slash escapes (//c/foo) for ...

58 0
View
vitest-3-features logo
JosiahSiegel/claude-plugin-marketplace

vitest-3-features

Vitest 3 features and migration from Vitest 2 (2025). PROACTIVELY activate for: (1) Vitest 3 stable features, (2) workspace mode for monorepos, (3) test annotations and reporter API, (4) projects configuration, (5) browser mode in Vitest 3, (6) inline snapshots and toMatchInlineSnapshot, (7) type...

58 0
View
windows-git-bash-paths logo
JosiahSiegel/claude-plugin-marketplace

windows-git-bash-paths

Windows and Git Bash path handling for SSDT, SqlPackage, and DACPAC files. PROACTIVELY activate for: (1) SqlPackage failing on Git Bash with path errors, (2) DACPAC file path conversion (MSYS, MINGW), (3) shell detection (PowerShell vs Git Bash) for SSDT scripts, (4) MSYS_NO_PATHCONV usage, (5) W...

58 0
View
debugging-troubleshooting-2025 logo
JosiahSiegel/claude-plugin-marketplace

debugging-troubleshooting-2025

Comprehensive bash script debugging and troubleshooting techniques for 2025. PROACTIVELY activate for: (1) debugging a misbehaving bash script, (2) using set -x, set -e, set -u, set -o pipefail (strict mode), (3) customizing PS4 for richer trace output, (4) trap DEBUG, ERR, and EXIT for diagnosti...

58 0
View
tailwindcss-accessibility logo
JosiahSiegel/claude-plugin-marketplace

tailwindcss-accessibility

Tailwind CSS accessibility patterns including WCAG 2.2 compliance, touch targets, focus management, and ARIA. PROACTIVELY activate for: (1) building accessible UI with Tailwind, (2) WCAG 2.2 compliance (Level A, AA, AAA), (3) focus rings and focus-visible styling, (4) minimum touch-target sizes (...

58 0
View
git-2-49-features logo
JosiahSiegel/claude-plugin-marketplace

git-2-49-features

Git 2.49 specific features and performance improvements. PROACTIVELY activate for: (1) git-backfill command (new in 2.49) for partial clones, (2) path-walk API improvements, (3) zlib-ng integration for faster compression, (4) reftables backend stabilization, (5) revision-walk performance, (6) bun...

58 0
View
sql-server-2025 logo
JosiahSiegel/claude-plugin-marketplace

sql-server-2025

SQL Server 2025 and SqlPackage 170.2.70 (October 2025) — vector databases, AI integration, and modern features. PROACTIVELY activate for: (1) SQL Server 2025 features, (2) vector type and vector indexes for AI, (3) Vector functions (VECTOR_DISTANCE, etc.), (4) AI integration (Azure OpenAI, embedd...

58 0
View
docker-security-guide logo
JosiahSiegel/claude-plugin-marketplace

docker-security-guide

Comprehensive Docker security guidelines and threat mitigation. PROACTIVELY activate for: (1) container security review, (2) running as non-root user, (3) read-only root filesystems and tmpfs mounts, (4) capability dropping (--cap-drop ALL), (5) seccomp and AppArmor profiles, (6) image vulnerabil...

58 0
View
powershell-security logo
JosiahSiegel/claude-plugin-marketplace

powershell-security

Modern PowerShell security practices for 2025 — SecretManagement, JEA, WDAC, credential protection. PROACTIVELY activate for: (1) Microsoft.PowerShell.SecretManagement and SecretStore for credential storage, (2) Just Enough Administration (JEA) endpoints, (3) Windows Defender Application Control ...

58 0
View
nextjs-middleware logo
JosiahSiegel/claude-plugin-marketplace

nextjs-middleware

Complete Next.js Middleware system (Next.js 15.5/16). PROACTIVELY activate for: (1) Creating middleware.ts (Edge) or proxy.ts (Node.js), (2) Matcher configuration, (3) Authentication protection, (4) Role-based access control, (5) Redirects and rewrites, (6) Internationalization (i18n), (7) Securi...

58 0
View
nextjs-data-fetching logo
JosiahSiegel/claude-plugin-marketplace

nextjs-data-fetching

Complete Next.js data fetching system (Next.js 15.5/16). PROACTIVELY activate for: (1) Server Component data fetching, (2) Parallel and sequential fetching, (3) Streaming with Suspense, (4) Route Handlers (API routes), (5) Client-side fetching with SWR/TanStack Query, (6) generateStaticParams for...

58 0
View
advanced-features-2025 logo
JosiahSiegel/claude-plugin-marketplace

advanced-features-2025

Advanced Claude Code plugin features for hooks, MCP integration, team distribution, and progressive disclosure. PROACTIVELY activate for: (1) progressive disclosure design, (2) hook automation, (3) PreToolUse and PostToolUse hooks, (4) MCP server integration, (5) .mcp.json configuration, (6) team...

58 0
View

Popular AI tools

Kaiber logo
Video

Kaiber

Generate, edit, and beat-sync AI video with leading models in one workspace.

Paid
View
Vimcal logo
Productivity

Vimcal

The world's fastest calendar for remote work

Free
View

Transform Your Design with AI Designer by ImgCreator.ai

Freemium
View
Akool AI logo
Content & writing

Akool AI

Revolutionizing Video Production with AI-Powered Creativity

Paid
View

Extend an image past the frame and let AI fill the new aspect ratio.

Freemium
View
StarByFace logo
Security

StarByFace

Discover your celebrity doppelgänger with StarByFace!

Free
View
C

ChainClarity explains 700+ crypto whitepapers in plain English, with layered summaries, comparisons, research tools, alerts, and a $4.99 Pro plan.

Freemium
View
Opus Clip logo
Coding & apps

Opus Clip

Opus.ai: Revolutionize Your Web Experience

Free
View