Deviopath › Next.js

Match a Dynamic Route

Easy · Routing

A folder named [slug] is a dynamic segment: app/blog/[slug]/page.tsx matches /blog/hello, /blog/news, and so on. The page then gets params with the matched value, like { slug: "hello" }.

Write getParams(pattern, pathname):

- Split both into segments by / (ignore empty segments).

- The number of segments must be the same.

- A segment like [name] matches any value and stores it as params.name.

- Every other segment must be exactly equal.

- Return the params object (it can be {}), or null when the path does not match.

Next: Highlight the Active Link