What is Dynamic Routing?
-
Dynamic routing lets you create pages with variable paths.
-
Instead of hardcoding all URLs, you define dynamic segments that act as placeholders.
-
Perfect for URLs like
/posts/1,/users/john, or/products/shoes.
How to Create Dynamic Routes?
-
Use square brackets in the filename inside the
pagesfolder.
Example:
-
[id].jsbecomes a dynamic page whereidis the variable part of the URL.
Accessing Dynamic Route Parameters
In the dynamic page, use useRouter or getStaticProps / getServerSideProps to access the parameter.
Example: pages/posts/[id].js
Dynamic Routes with Static Generation (getStaticPaths)
When using Static Site Generation (SSG) with dynamic routes, you need to specify which paths to pre-render.
Example:
Catch-All Routes
-
For nested or variable-length URLs, use
[...param].js.
Example:
-
URL
/blog/2025/08/02/titlepassesslug = ['2025', '08', '02', 'title'].
Summary
| Feature | How to Use |
|---|---|
| Dynamic Routes | Use [param].js in pages/ |
| Access Route Params | useRouter or data fetching methods |
| Static Generation Paths | Use getStaticPaths with params |
| Catch-All Routes | Use [...param].js for nested paths |
