A blog may have a thousand posts. You do not want a thousand folders. You want one page that can show any post, depending on the address. This is what a dynamic route does. Think of a hotel with one reception desk for all the rooms. The guest says a room number and the desk gives the right key. The room number is the dynamic part of the address.
A dynamic segment
Wrap a folder name in square brackets. A folder called [slug] matches any single value in that position. The value is handed to your page as a prop called params. In Next.js 16 params is a Promise, so you await it. This is a change from older versions, which you may still see in older tutorials.
export function generateStaticParams() {
return [{ slug: "hello-nextjs" }, { slug: "routing-basics" }];
}
export default async function PostPage({ params }: PageProps<"/blog/[slug]">) {
const { slug } = await params;
return <h1>Post: {slug}</h1>;
}
- app/blog/[slug]/page.tsx answers /blog/hello-nextjs and /blog/anything-else.
- PageProps is a type that Next.js generates for you. The text inside the brackets must match the folder, so TypeScript knows that slug exists.
- await params gives an object, here with the key slug.
- generateStaticParams is optional. It tells Next.js which values to build ahead of time. Here two posts are prepared at build time, so they are very fast.
What happens for an address that is not in the list? We tested it. Next.js built /blog/hello-nextjs and /blog/routing-basics at build time. When we asked for /blog/not-in-list, it still answered with a page, because by default a value that was not prepared is rendered the first time someone asks. If you want such addresses to show a not-found page, check the value in your page and call notFound, which you will learn in lesson 9. Older Next.js versions had a setting called dynamicParams for this, but the official docs say it is not available when Cache Components is turned on, which is the default in a new Next.js 16 project.
Several dynamic segments
You can use more than one bracket folder in a path. A path such as app/shop/[category]/[product]/page.tsx matches /shop/shoes/running-shoe. Then params has two keys, category and product, and you read them the same way.
export default async function ProductPage({ params }: PageProps<"/shop/[category]/[product]">) {
const { category, product } = await params;
return <p>{category} / {product}</p>;
}
Catch-all routes
Sometimes you do not know how many parts the address has. A documentation site may have /docs/a, /docs/a/b and /docs/a/b/c. Use three dots inside the brackets: [...slug]. The value is an array of all the remaining parts.
import { Suspense } from "react";
async function Parts({ params }: { params: PageProps<"/shop/[...slug]">["params"] }) {
const { slug } = await params; // always an array, for example ["shoes", "running"]
return <p>Path parts: {slug.join(" / ")}</p>;
}
export default function ShopPage(props: PageProps<"/shop/[...slug]">) {
return (
<Suspense fallback={<p>Loading...</p>}>
<Parts params={props.params} />
</Suspense>
);
}
We ran this page. The address /shop/shoes/running gave the text Path parts: shoes / running. A catch-all needs at least one part, so /shop alone does not match it.
Optional catch-all routes
If the plain address should also work, use double brackets: [[...slug]]. Now /docs, /docs/a and /docs/a/b all match. For /docs the value is undefined, so the page must handle that.
import { Suspense } from "react";
async function Doc({ params }: { params: PageProps<"/docs/[[...slug]]">["params"] }) {
const { slug } = await params; // undefined on /docs itself
return <p>{slug ? `Doc: ${slug.join("/")}` : "Docs home"}</p>;
}
export default function DocsPage(props: PageProps<"/docs/[[...slug]]">) {
return (
<Suspense fallback={<p>Loading...</p>}>
<Doc params={props.params} />
</Suspense>
);
}
In our test /docs showed Docs home and /docs/a/b showed Doc: a/b.
GET / 200
GET /about 200
GET /blog/hello-nextjs 200
GET /blog/not-in-list 200 (not listed in generateStaticParams, rendered when asked)
GET /shop/shoes/running 200 (Path parts: shoes / running)
GET /docs 200 (Docs home)
GET /docs/a/b 200 (Doc: a/b)
GET /pricing 200 (the (marketing) folder is not part of the URL)
GET /old-page 307 -> /about
GET /nope 404 (our not-found.tsx page)
GET /products/1 200 (title: Notebook, description: A plain paper notebook.)
GET /dashboard 200 (layout sidebar + Dashboard overview)
GET /posts/hello-nextjs 200
GET /posts/zzz 200 (our not-found page, noindex tag added, status still 200 because the response was streamed)
GET /old-blog/hello-nextjs 308 -> /blog/hello-nextjs (permanent redirect from next.config.ts)
GET /shop-multi/shoes/running-shoe 200
Which one should I use?
- [slug]: exactly one value. Posts, products, users.
- [...slug]: one or more values. Category trees where the address needs at least one part.
- [[...slug]]: zero or more values. A documentation home page and all its sub-pages in one file.
Take care with order
If a fixed folder and a dynamic folder could both match, the fixed one wins. A folder called app/blog/new is used for /blog/new, and [slug] answers everything else. This lets you add special pages without breaking the general one.
Your task
- Make /blog/[slug] and show the slug in a heading. Add generateStaticParams with two slugs.
- Make /shop/[...slug] and show the parts joined with a slash.
- Make /docs/[[...slug]] and check that /docs, /docs/a and /docs/a/b all work.
- Add app/blog/new/page.tsx and see which page answers /blog/new.
How to check it worked
- Different slugs show different content from one file.
- The catch-all shows the parts as an array.
- /docs works with the optional catch-all but not with the normal one.