Dynamic Routes

Examples

Defining routes by using predefined paths is not always enough for complex applications. In Next.js you can add brackets to a page ([param]) to create a dynamic route (a.k.a. url slugs, pretty urls, and others).

Consider the following page pages/post/[pid].js:

  1. import { useRouter } from 'next/router'
  2. const Post = () => {
  3. const router = useRouter()
  4. const { pid } = router.query
  5. return <p>Post: {pid}</p>
  6. }
  7. export default Post

Any route like /post/1, /post/abc, etc. will be matched by pages/post/[pid].js. The matched path parameter will be sent as a query parameter to the page, and it will be merged with the other query parameters.

For example, the route /post/abc will have the following query object:

  1. { "pid": "abc" }

Similarly, the route /post/abc?foo=bar will have the following query object:

  1. { "foo": "bar", "pid": "abc" }

However, route parameters will override query parameters with the same name. For example, the route /post/abc?pid=123 will have the following query object:

  1. { "pid": "abc" }

Multiple dynamic route segments work the same way. The page pages/post/[pid]/[comment].js will match the route /post/abc/a-comment and its query object will be:

  1. { "pid": "abc", "comment": "a-comment" }

Client-side navigations to dynamic routes are handled with next/link. If we wanted to have links to the routes used above it will look like this:

  1. import Link from 'next/link'
  2. function Home() {
  3. return (
  4. <ul>
  5. <li>
  6. <Link href="/post/abc">
  7. <a>Go to pages/post/[pid].js</a>
  8. </Link>
  9. </li>
  10. <li>
  11. <Link href="/post/abc?foo=bar">
  12. <a>Also goes to pages/post/[pid].js</a>
  13. </Link>
  14. </li>
  15. <li>
  16. <Link href="/post/abc/a-comment">
  17. <a>Go to pages/post/[pid]/[comment].js</a>
  18. </Link>
  19. </li>
  20. </ul>
  21. )
  22. }
  23. export default Home

Read our docs for Linking between pages to learn more.

Catch all routes

Examples

Dynamic routes can be extended to catch all paths by adding three dots (...) inside the brackets. For example:

  • pages/post/[...slug].js matches /post/a, but also /post/a/b, /post/a/b/c and so on.

Note: You can use names other than slug, such as: [...param]

Matched parameters will be sent as a query parameter (slug in the example) to the page, and it will always be an array, so, the path /post/a will have the following query object:

  1. { "slug": ["a"] }

And in the case of /post/a/b, and any other matching path, new parameters will be added to the array, like so:

  1. { "slug": ["a", "b"] }

Optional catch all routes

Catch all routes can be made optional by including the parameter in double brackets ([[...slug]]).

For example, pages/post/[[...slug]].js will match /post, /post/a, /post/a/b, and so on.

The main difference between catch all and optional catch all routes is that with optional, the route without the parameter is also matched (/post in the example above).

The query objects are as follows:

  1. { } // GET `/post` (empty object)
  2. { "slug": ["a"] } // `GET /post/a` (single-element array)
  3. { "slug": ["a", "b"] } // `GET /post/a/b` (multi-element array)

A good example of optional catch all routes is the Next.js docs, a single page called pages/docs/[[…slug]].js takes care of all the docs you’re currently looking at.

Caveats

  • Predefined routes take precedence over dynamic routes, and dynamic routes over catch all routes. Take a look at the following examples:

    • pages/post/create.js - Will match /post/create
    • pages/post/[pid].js - Will match /post/1, /post/abc, etc. But not /post/create
    • pages/post/[...slug].js - Will match /post/1/2, /post/a/b/c, etc. But not /post/create, /post/abc
  • Pages that are statically optimized by Automatic Static Optimization will be hydrated without their route parameters provided, i.e query will be an empty object ({}).

    After hydration, Next.js will trigger an update to your application to provide the route parameters in the query object.

Related

For more information on what to do next, we recommend the following sections:

[

Pages

Enable client-side transitions with next/link.]($6a9658b70d6fcd3d.md)

[

Routing

Learn more about routing in Next.js.]($7122afeb1eed2632.md)