Next.js
frontendnextjsGuideBeginner
Dynamic Routes and Catch-All Segments in Next.js
Mastering dynamic routing: [slug], catch-all [...slug], optional catch-all [[...slug]], generateStaticParams(), and static site generation.
Verified: 2026-09-30
Tested on:nextjs 16.x
Platforms:
web
#nextjs#dynamic-routes#catch-all#generate-static-params#ssg
Dynamic Routes and Catch-All Segments in Next.js
When you don't know the exact URL segment names ahead of time (e.g. blog posts, product IDs, documentation paths), use Dynamic Route Segments wrapped in square brackets [slug].
1. Dynamic Routing Conventions
| Folder Pattern | Route Example | Parameter Extracted |
|---|---|---|
app/blog/[slug]/page.tsx | /blog/react-hooks | { slug: "react-hooks" } |
app/docs/[...slug]/page.tsx (Catch-all) | /docs/git/branch/merge | { slug: ["git", "branch", "merge"] } |
app/docs/[[...slug]]/page.tsx (Optional catch-all) | /docs AND /docs/git/branch | { slug: undefined } OR { slug: ["git", "branch"] } |
2. Static Generation with generateStaticParams()
Generate static HTML files for all dynamic routes at build time (SSG) for maximum performance:
// app/docs/[[...slug]]/page.tsx
import { notFound } from "next/navigation";
import { docsSource } from "@/app/source";
// 1. Tell Next.js all possible slug paths at build time:
export async function generateStaticParams() {
return docsSource.generateParams();
}
// 2. Render pre-generated static page:
export default async function DocPage({
params,
}: {
params: Promise<{ slug?: string[] }>;
}) {
const { slug } = await params;
const page = docsSource.getPage(slug);
if (!page) notFound();
return (
<article>
<h1>{page.data.title}</h1>
<p>{page.data.description}</p>
</article>
);
}Related Topics
Pages in Next.js App Router
Creating pages in Next.js: page.tsx conventions, PageProps typing, async Server Components, searchParams, and params.
Route Groups in Next.js App Router
Organizing routes with Route Groups (folderName): omitting folder names from URLs, creating multiple root layouts, and organizing dashboard vs marketing routes.