Catch-all routes do not catch the root index route
See original GitHub issueFeature Request
Describe the use case
I have a use case where I need all of the following requests to go to the same function:
POST pages/api/users
PATCH pages/api/users/1
DELETE pages/api/users/1
After reading the docs, I was sure I could catch all of those using this:
pages/api/users/[...slug].js
However, this does not catch POST pages/api/users
without an explicit /index.js
.
To Reproduce
Try the above scenario (even with GET requests)
Expected behavior
This route:
pages/api/users/[...slug].js
Catches all of these:
POST pages/api/users
PATCH pages/api/users/1
DELETE pages/api/users/1
Screenshots
N/A
System information
N/A
Additional context
Next.js 9.2.2-canary.15
Issue Analytics
- State:
- Created 4 years ago
- Reactions:48
- Comments:38 (25 by maintainers)
Top Results From Across the Web
Spring catch all route for index.html - Stack Overflow
I'm developing a spring backend for a react-based single page application where I'm using react-router for client-side routing. Beside the index.html page the ......
Read more >ASP.NET Core Blazor routing and navigation - Microsoft Learn
Catch-all route parameters, which capture paths across multiple folder boundaries, are supported in components. Catch-all route parameters are:.
Read more >Next.js Optional catch all routes - GeeksforGeeks
In this article, we will learn how we can optional catch-all routes in our NextJS project. NextJS is a React-based framework.
Read more >Working with routes for HTTP APIs - Amazon API Gateway
You can create a $default route that acts as a catch-all for requests that don't match any other routes. Working with path variables....
Read more >Routing • Docs • SvelteKit
src/routes is the root route; src/routes/about creates an /about route ... +error.svelte is not used when an error occurs inside handle or a...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
@timneutkens created https://github.com/zeit/next.js/pull/10502
Been thinking a bit more about this issue and I think the requested behavior could make more sense for the following reasons:
Conceptually
[...slug]
looks like array destructuring into an array. Arrays can have zero items, so why can’tslug
have zero items? The index route would just receive an empty array as theslug
parameter.The solution I proposed is flawed. Let me give an example:
Current situation, imagine we have a catch-all route like:
And we fetch the following urls:
getPost
called with/api/posts
/api/posts/index
resolves, so why not this?/api/posts/index
['index']
/api/posts/something
['something']
/api/posts/something/else
['something', 'else']
Imagine we add a index route:
now this behavior becomes
getPost
called with/api/posts
undefined
/api/posts/index
undefined
/api/posts/something
['something']
/api/posts/something/else
['something', 'else']
Now, imagine
/api/posts/[...slug].js
would also be called for the index route, like this issue proposes. Then with just the following routethe behavior would be more predictable and uniform and zen
getPost
called with/api/posts
[]
/api/posts/index
['index']
/api/posts/something
['something']
/api/posts/something/else
['something', 'else']
I’ll keep the issue open to see if there’s anyone else asking for it.