How do you create dynamic routes in Next.js?

Wrap a folder name in square brackets, such as app/blog/[slug]/page.tsx. The matching URL part arrives in the params prop, which is a Promise since Next.js 15 and must be awaited.

App Router

  • app/blog/[slug]/page.tsx matches /blog/hello with params = { slug: "hello" }.
  • Multiple segments work: app/shop/[category]/[id]/page.tsx.
  • generateStaticParams returns the values to prerender at build time.
  • Unknown values: with Cache Components, a param not returned by generateStaticParams serves the App Shell immediately and the full page is generated in the background, then cached (ISR). Without Cache Components, export const dynamicParams = false returns 404 for unknown values.
  • In Client Components, read params with useParams() from next/navigation.

Why params is a Promise

Making params async lets Next.js prerender everything that does not depend on it. You can even pass the promise down and await it inside a <Suspense> boundary so the rest of the layout stays in the static shell. Synchronous access was fully removed in Next.js 16.

Typing

Use the generated helper PageProps<'/blog/[slug]'> (from next typegen) instead of hand-writing { params: Promise<{ slug: string }> }.

Pages Router

pages/blog/[slug].tsx, reading params in getStaticProps/getServerSideProps or router.query.slug on the client.

Code example

tsx
1// app/blog/[slug]/page.tsx
2import { notFound } from "next/navigation";
3import { cacheLife } from "next/cache";
4
5async function getPost(slug: string) {
6 "use cache";
7 cacheLife("days");
8 const res = await fetch(`https://cms.example.com/posts/${slug}`);
9 return res.ok ? res.json() : null;
10}
11
12// Prerender the most popular posts at build time
13export async function generateStaticParams() {
14 const posts = await fetch("https://cms.example.com/posts?top=20").then((r) => r.json());
15 return posts.map((p: { slug: string }) => ({ slug: p.slug }));
16}
17
18export default async function Post({ params }: PageProps<"/blog/[slug]">) {
19 const { slug } = await params;
20 const post = await getPost(slug);
21 if (!post) notFound();
22
23 return <h1>{post.title}</h1>;
24}

Practice this question

Open it in the study app to track progress, take notes, listen to the answer and work through the rest of the Next.js set.

Open in study app