typescriptintermediate
Catch-All and Optional Catch-All Routes
Handle dynamic nested routes with catch-all [...slug] and optional [[...slug]] segments.
typescriptPress ⌘/Ctrl + Shift + C to copy
// app/docs/[[...slug]]/page.tsx
import { notFound } from 'next/navigation';
interface DocPage {
title: string;
content: string;
}
async function getDoc(slugParts: string[]): Promise<DocPage | null> {
const path = slugParts.join('/');
const res = await fetch(`https://api.example.com/docs/${path || 'index'}`);
if (!res.ok) return null;
return res.json();
}
export async function generateStaticParams() {
const res = await fetch('https://api.example.com/docs/all-paths');
const paths: string[][] = await res.json();
return [
{ slug: [] },
...paths.map((segments) => ({ slug: segments })),
];
}
export const dynamicParams = false;
export default async function DocsPage({
params,
}: {
params: Promise<{ slug?: string[] }>;
}) {
const { slug = [] } = await params;
const doc = await getDoc(slug);
if (!doc) notFound();
return (
<article className="prose dark:prose-invert">
<h1>{doc.title}</h1>
<div dangerouslySetInnerHTML={{ __html: doc.content }} />
</article>
);
}Use Cases
- documentation sites
- nested content pages
- CMS-driven routes
Tags
Related Snippets
Similar patterns you can reuse in the same workflow.
typescriptadvanced
Parallel Routes Layout
Next.js parallel routes pattern to render multiple page slots simultaneously in a shared layout.
Best for: Dashboard multi-panel layouts
#parallel-routes#layout
typescriptadvanced
Next.js Parallel Routes Layout
Use parallel routes with named slots to render multiple pages simultaneously in a layout.
Best for: Dashboard layouts with independent loading states
#nextjs#routing
typescriptadvanced
Next.js Intercepting Routes for Modals
Implement modal patterns with intercepting routes that work with both soft and hard navigation.
Best for: Image gallery modals with shareable URLs
#nextjs#routing
typescriptintermediate
Route Groups for Layout Organization
Use route groups to share layouts across routes without affecting URL structure.
Best for: Marketing vs. dashboard layouts
#nextjs#routing