Dynamic routes and params
Square-bracket segments, catch-all and optional catch-all routes, awaiting params and searchParams in Next.js 16, and the typed PageProps helper.
Dynamic routes and params
The player loads from youtube-nocookie.com when you press play. Nothing reaches YouTube before that.
Not started
What you'll learn
- A folder named in square brackets, such as `[slug]`, matches any one URL segment and hands its value to the page as a param; `[...slug]` matches one or more segments and `[[...slug]]` matches zero or more.
- In Next.js 15 and 16, `params` and `searchParams` are Promises, so a page must `await` them (or `use()` them in a Client Component) before reading values.
- Reading `searchParams` makes a page dynamic, because the value only exists when a request arrives; reading `params` for URLs not known at build time has the same effect.
- `generateStaticParams` tells Next.js which param values to prerender at build time, and `PageProps<'/blog/[slug]'>` gives typed access to params.
One page, many URLs
A blog has hundreds of posts, and they all look the same. You do not want hundreds of folders. You want a single page that can say, “whatever came after /blog/ is the post to show.”
Wrap a folder name in square brackets and that segment becomes a parameter:
app/blog/[slug]/page.tsx → /blog/hello, /blog/routing, /blog/anything
The value is passed to the page in a params prop. In Next.js 16 it is a Promise, so you await it:
// app/blog/[slug]/page.tsx
import { notFound } from "next/navigation";
const posts: Record<string, string> = {
hello: "Hello, world",
routing: "File routing",
};
export default async function Page({ params }: PageProps<"/blog/[slug]">) {
const { slug } = await params;
const title = posts[slug];
if (!title) notFound();
return <h1>{title}</h1>;
}
PageProps<"/blog/[slug]"> is a global type helper that Next.js generates from your folder structure. It types params as Promise<{ slug: string }> and gives you searchParams too. If you prefer, type the prop by hand: { params: Promise<{ slug: string }> }.
Catch-all segments
Two variants cover paths of unknown depth:
| Folder | /shop |
/shop/clothing |
/shop/clothing/shirts |
|---|---|---|---|
[slug] |
no match | { slug: 'clothing' } |
no match |
[...slug] |
no match | { slug: ['clothing'] } |
{ slug: ['clothing', 'shirts'] } |
[[...slug]] |
{} |
{ slug: ['clothing'] } |
{ slug: ['clothing', 'shirts'] } |
A catch-all gives you an array, and the double-bracket form also matches the bare path. A documentation site, where /docs, /docs/a and /docs/a/b all use one page, is the classic use.
Why params is a Promise
Earlier Next.js versions handed you params as a plain object. From 15 on it is async, and in 16 the synchronous access is gone. The reason is rendering strategy: if every request-specific value is a Promise, the framework can start rendering the static parts of a page before it knows what the request contains, and only suspend where a component actually awaits. You will see that pay off in static and dynamic rendering and partial prerendering.
In a Client Component you cannot make the function async, so unwrap the Promise with React’s use: const { slug } = use(params). See the React roadmap’s the use hook.
Search params
Query strings, such as /shop?sort=price, arrive in the searchParams prop, also a Promise, on pages only (not layouts):
export default async function Page({ searchParams }: PageProps<"/shop">) {
const { sort } = await searchParams;
return <p>Sorted by {sort ?? "default"}</p>;
}
Reading searchParams opts the page into dynamic rendering, because a query string only exists when a request arrives. For client-only needs, such as filtering a list already loaded, use the useSearchParams hook instead.
Prerendering dynamic routes
When Next.js builds a dynamic route it does not know which values will occur. generateStaticParams tells it:
export function generateStaticParams() {
return Object.keys(posts).map((slug) => ({ slug }));
}
Those pages are built ahead of time. A real build of the example above lists /blog/hello and /blog/routing as prerendered, while the wider /blog/[slug] route stays a partial prerender with the rest rendered on request.
One practical detail about the Next.js 16 scaffold, which turns on Cache Components. If you await params in a route with no generateStaticParams and no loading boundary above it, next build fails with “Next.js encountered uncached or runtime data during prerendering”, and the fix it suggests is a <Suspense> boundary, caching with "use cache", or generateStaticParams. A loading.tsx in a parent folder counts as that Suspense boundary. This is covered in full in the data-fetching and rendering groups.
What the video covers
Codevolution’s chapters build a product listing and detail page: nested routes, a products folder, implementing the dynamic segment, fetching and displaying product details, and a visualization of how dynamic routes map to URLs. It is Next.js 15, where params was already a Promise, so the await pattern matches. The params and search params follow-up and the catch-all segments video are linked under deeper.
Where this fits
Dynamic segments handle one URL pattern. Next, route groups and private folders lets you organize the tree without changing the URLs.
Check yourself
What are the three kinds of dynamic segment folders, and how does each match `/shop/clothing/shirts`?
`[slug]` matches exactly one segment, so it would match `/shop/clothing` but not the longer path. `[...slug]` matches one or more, producing `['clothing', 'shirts']`. `[[...slug]]` matches the same and also matches `/shop` with no extra segments.
Why do you have to `await params` in Next.js 16?
`params` is a Promise. Next.js made request-time values asynchronous so it can start rendering and prerendering before it knows which request-specific values a component will read. Reading a property directly without awaiting gives you the Promise, not the value.
How do you make a dynamic route prerender at build time instead of rendering on each request?
Export `generateStaticParams` from the segment, returning the list of param objects to prerender, such as one `{ slug }` per blog post. Next.js then builds those pages ahead of time.
Go deeper
Notes
Notes are saved to your account.
Everything else on this page works without one — progress and bookmarks keep themselves in this browser and come with you when you sign in. Notes are the one thing worth not trusting to a browser you might clear.
Your notes could not be loaded.