By including the following code snippet in your route:
[...pageParams].tsx
Next.js will capture all paths under that route, such as article/a, article/a/b, article/a/b/c, and so on.
If you have another dynamic route (slug) under the same parent route, it will also be captured by the catch-all path ...pageParams.
To learn more about catch-all routes, refer to this section of the documentation:
Dynamic routes can be expanded to capture all paths by adding three dots (...) inside the brackets. For example:
pages/post/[...slug].js matches /post/a, as well as /post/a/b, /post/a/b/c, and so forth
In my approach, I typically set up a static route /page with a dynamic route for the page number structured like this:
[categories]/page/[number].tsx
[categories]/[slug].tsx
Next.js will prioritize the dynamic route /page/[slug] over /[slug]
If you need to handle the route /page (interpreted as a slug), you can set up a redirect in next.config.js
redirects() {
return [
{
source: "/:categories/page",
destination: "/:categories/page/1",
permanent: true
}
]
}
This configuration ensures that if a user accesses the /page URL, they will be redirected to /page/1