How do dynamic routes and notFound() work in the Next.js App Router?

IntermediateNext.js interview questionBy , updated 21 November 2026

Short answer

A folder name in square brackets, such as app/blog/[slug], is a dynamic segment: it matches any value in that position and passes it to the page as params. [...slug] catches one or more segments and [[...slug]] also matches zero. In current Next.js, params is a Promise you must await. When the value does not match real data, call notFound(), which renders the nearest not-found.tsx and returns a 404 if the response has not started streaming.

The three kinds of dynamic segment

Folder Matches params
app/blog/[slug]/page.tsx /blog/a { slug: "a" }
app/shop/[...slug]/page.tsx /shop/a, /shop/a/b { slug: ["a"] }, { slug: ["a", "b"] }
app/shop/[[...slug]]/page.tsx /shop, /shop/a, /shop/a/b { slug: undefined }, { slug: ["a"] }, ...

A catch-all does not match the bare parent path: /shop is a 404 for [...slug] unless app/shop/page.tsx exists. The optional catch-all covers it. Segments combine, so app/[category]/[item]/page.tsx gives { category, item }.

params is passed to page, layout, route and generateMetadata. Values are always strings or arrays of strings, never numbers, because they come straight from the URL.

params is a Promise

// app/blog/[slug]/page.tsx
import { notFound } from "next/navigation";

export default async function PostPage({ params }: PageProps<"/blog/[slug]">) {
  const { slug } = await params;
  const post = await getPost(slug);

  if (!post) notFound();

  return <article><h1>{post.title}</h1></article>;
}

Next.js 15 made params and searchParams Promises, with temporary synchronous access for compatibility. Next.js 16 removed the synchronous access, so params.slug without await no longer works. In a Client Component page, unwrap it with React's use(params), or read the values anywhere in client code with useParams().

PageProps<"/blog/[slug]"> is a global type helper generated by Next.js; LayoutProps and RouteContext do the same for layouts and Route Handlers. Since a user can type any URL, validate the value before trusting it, as the notFound() check does here.

Prerendering with generateStaticParams

By default a dynamic route renders on request. To prerender known values at build time, export generateStaticParams:

export async function generateStaticParams() {
  const posts = await getAllPosts();
  return posts.map((post) => ({ slug: post.slug }));
}

export const dynamicParams = true; // the default

It returns one object per page, and replaces getStaticPaths from the Pages Router. dynamicParams replaces fallback:

  • true (default): a slug not in the list renders on its first request.
  • false: a slug not in the list returns a 404.

With Cache Components enabled, dynamicParams is not available. Params not returned by generateStaticParams are then runtime data, and reading them needs a <Suspense> boundary or loading.tsx around the part that awaits them. For how this fits with caching, see SSR vs SSG vs ISR.

How notFound() works

notFound() from next/navigation throws a special error. It stops rendering the segment, and the nearest not-found.tsx above it renders in its place. A few details:

  • No return needed. It throws, and TypeScript types it as never, so post is narrowed after the check.
  • It works in Server Components, Server Functions and Route Handlers. In a Route Handler it sends a 404 response.
  • Do not wrap it in try/catch. Catching the error swallows it and the 404 UI never renders. If you must catch near the call, use unstable_rethrow first.
  • Status code depends on timing. Called before the response starts, the status is 404. Called inside a <Suspense> boundary after the shell has streamed, the status stays 200, but Next.js still swaps in the not-found UI. In both cases it adds <meta name="robots" content="noindex">.

So for real 404s that search engines should see, do the existence check before streaming starts, at the top of the page or in a data function it awaits outside Suspense.

not-found.tsx

// app/blog/[slug]/not-found.tsx
import Link from "next/link";

export default function NotFound() {
  return (
    <section>
      <h1>Post not found</h1>
      <Link href="/blog">Back to the blog</Link>
    </section>
  );
}

It takes no props, so it cannot read the slug. It is a Server Component by default and can be async to fetch data. Without one in the segment, the nearest parent's renders, falling back to the default 404 page. The root app/not-found.tsx also handles every URL that matches no route. Where it sits among the other special files is covered in layout, loading and error files.

Common follow-up questions

How is this different from the Pages Router?

There, pages/blog/[slug].js was a file, data came from getStaticProps with getStaticPaths, and you returned { notFound: true }. See App Router vs Pages Router.

Can I read params in a layout?

Yes. A layout receives params for its own dynamic segment and the ones above it, as a Promise like the page's. It does not receive searchParams, because layouts do not rerender on navigation.

What about a 403 or 401?

forbidden() and unauthorized() work like notFound() with forbidden.tsx and unauthorized.tsx, but they are experimental and need experimental.authInterrupts.

How do I practise route matching?

Mistakes to avoid

  • Reading params.id without awaiting params

  • Treating params.id as a number, or trusting it without validation

  • Wrapping notFound() in a try/catch that swallows it

  • Calling notFound() deep inside a Suspense boundary and expecting a 404 status

  • Using [...slug] when the bare path should also match; use [[...slug]]

  • Setting dynamicParams = false and then wondering why new content returns 404 until the next build

Practise it in code

Interviewers rarely stop at the definition. These questions open in an editor with tests.

  1. Build Next.js App Router routing from files: dynamic and catch-all segments, route groups and conflictsMedium40 min
  2. Build a route matcher with params, optional segments and wildcards, ranked by specificityMedium35 min
  3. Extract typed route params from a path pattern like "/users/:id/posts/:postId"Hard50 min
  4. Choose a rendering strategy per route for a marketplaceHard50 min