Dynamic Routes
Dynamic Routes can be used to programmatically generate route segments from dynamic data.
Convention
A Dynamic Segment can be created by wrapping a folder's name in square brackets: [folderName]
. For example, [id]
or [slug]
.
Dynamic Segments are passed as the params
prop to layout
and page
.
Example
For example, a blog could include the following route /src/routes/blog/[slug]/page.js
where [slug]
is the Dynamic Segment for blog posts.
Route | Example URL | params |
---|---|---|
/src/routes/blog/[slug]/page.js | /blog/a | { slug: 'a' } |
/src/routes/blog/[slug]/page.js | /blog/b | { slug: 'b' } |
/src/routes/blog/[slug]/page.js | /blog/c | { slug: 'c' } |
Catch-all Segments
Dynamic Segments can be extended to catch-all subsequent segments by adding an ellipsis inside the brackets [...folderName]
.
For example, /src/routes/shop/[...slug]/page.js
will match /shop/clothes
, but also /shop/clothes/tops
, /shop/clothes/tops/t-shirts
, and so on.
Route | Example URL | params |
---|---|---|
/src/routes/shop/[...slug]/page.js | /shop/a | { slug: ['a'] } |
/src/routes/shop/[...slug]/page.js | /shop/a/b | { slug: ['a', 'b'] } |
/src/routes/shop/[...slug]/page.js | /shop/a/b/c | { slug: ['a', 'b', 'c'] } |
TypeScript
When using TypeScript, you can add types for params
depending on your configured route segment.
Or just use type from package
Route | params Type Definition |
---|---|
/src/routes/blog/[slug]/page.js | { slug: string } |
/src/routes/shop/[...slug]/page.js | { slug: string[] } |
/src/routes/[categoryId]/[itemId]/page.js | { categoryId: string, itemId: string } |