#dynamic route takes over predefined one
1 messages · Page 1 of 1 (latest)
Hi @gaunt drift. I'm kapa.ai, your friendly support bot. I will try to answer your question by searching through the knowledge base. :wave:
In Next.js, predefined API routes take precedence over dynamic API routes. This means that if you have a dynamic route and a predefined route in your root folder, the predefined route will be matched first.
For example, consider the following routes:
pages/api/post/create.js- This will match/api/post/createpages/api/post/[pid].js- This will match/api/post/1,/api/post/abc, etc. But it will not match/api/post/create
In this case, if a request is made to /api/post/create, it will be handled by pages/api/post/create.js, not pages/api/post/[pid].js, even though [pid] is a dynamic segment that could match create.
This precedence rule ensures that specific routes are handled correctly before more general, dynamic routes.
You can find more information about this in the Next.js documentation on dynamic API routes (https://nextjs.org/docs/pages/building-your-application/routing/api-routes).
Note: you can ask me a follow up question by @ mentioning me again :speech_balloon:
@supple ibex kapa.ai is still learning and improving, please let us know how it did by reacting below
I talk about page routes, not api routes
@bold idol I talk about page routes, not api routes
Let me check the knowledge base for you. One moment! :books:
I apologize for the confusion. The same precedence rule applies to both API routes and page routes in Next.js. Predefined routes will always take precedence over dynamic routes.
For example, if you have the following page routes:
pages/post/create.js- This will match/post/createpages/post/[pid].js- This will match/post/1,/post/abc, etc. But it will not match/post/create
In this case, if a request is made to /post/create, it will be handled by pages/post/create.js, not pages/post/[pid].js, even though [pid] is a dynamic segment that could match create.
This precedence rule ensures that specific routes are handled correctly before more general, dynamic routes.
You can find more information about this in the Next.js documentation on dynamic routes (https://nextjs.org/docs/pages/building-your-application/routing/dynamic-routes).
Note: you can ask me a follow up question by @ mentioning me again :speech_balloon:
@supple ibex kapa.ai is still learning and improving, please let us know how it did by reacting below
Let me check the knowledge base for you. One moment! :books:
noopy va
In the context of the App Router in Next.js, the same precedence rule applies. The App Router takes priority over the Pages Router, and predefined routes take precedence over dynamic routes.
For instance, if you have the following routes in your app directory:
app/post/create.js- This will match/post/createapp/post/[pid].js- This will match/post/1,/post/abc, etc. But it will not match/post/create
In this case, if a request is made to /post/create, it will be handled by app/post/create.js, not app/post/[pid].js, even though [pid] is a dynamic segment that could match create.
This precedence rule ensures that specific routes are handled correctly before more general, dynamic routes.