next.js: Catch-all routes do not catch the root index route
Feature 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
About this issue
- Original URL
- State: closed
- Created 7 years ago
- Reactions: 48
- Comments: 38 (25 by maintainers)
Commits related to this issue
- Fix catch-all route + index.js in dev when accessed with trailing slash (#10502) Failing test case for https://github.com/zeit/next.js/issues/10488#issuecomment-584500081 This used to give a 500 in ... — committed to vercel/next.js by Janpot 7 years ago
- Fix catch-all route + index.js in dev when accessed with trailing slash (#10502) Failing test case for https://github.com/zeit/next.js/issues/10488#issuecomment-584500081 This used to give a 500 in ... — committed to chibicode/next.js by Janpot 7 years ago
- Fix catch-all route + index.js in dev when accessed with trailing slash (#10502) Failing test case for https://github.com/zeit/next.js/issues/10488#issuecomment-584500081 This used to give a 500 in ... — committed to rokinsky/next.js by Janpot 7 years ago
@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’tslughave zero items? The index route would just receive an empty array as theslugparameter.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:
getPostcalled with/api/posts/api/posts/indexresolves, 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
getPostcalled with/api/postsundefined/api/posts/indexundefined/api/posts/something['something']/api/posts/something/else['something', 'else']Now, imagine
/api/posts/[...slug].jswould 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
getPostcalled 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.
Now that blitz is in such high demand, could this feature be approved?
Below are the files for my tiny blog demo. The lack of this feature literally doubles the files needed. All of the
indexfiles are just re-exports of[...id]Currently:
After this feature:
The description by @Janpot is exactly the behaviour I expected and have been implementing with a now.json rewrite rule that weirdly only works on Now (not with
now dev). My use case is to capture all routes (including NO route) and pass them through to an headless CMS.EDIT: My new workaround is just to have an additional
index.tsxwith the following:This works with
now dev.Concretely, making a page named:
pages/hello/[[...world]].jswould match/hello,/hello/1, and/hello/2/3, etc.Today, making a page named:
pages/hello/[...world].jsonly matches/hello/1, and/hello/2/3, etc, but not/hello.We’re happy to take a PR if anyone wants to work on this.
I would also benefit from this feature. Right now another option seems to be implementing a custom routing which I think is overcomplicated for this case.
@timneutkens What about having another operator before the slug, something like pages/~[…slug].js. It will not break existing applications and it does not look obfuscated to me. I am willing to submit a PR if such an API looks reasonable to you.
I’m running into this as well, building out static pages with the new 9.3 SSG functions. To me, ideally, this would behave exactly as @Janpot described in the third option in their above comment.
For our architecture, we would basically have a
pages/[...path].jsxfile that would catch both the index page (if defined as a route fromgetStaticPaths) as well as any other pages defined by our CMS.@programbo the problem with that approach is because creates 2 lambdas one for […slug] and one for index.tsx
@timneutkens ok, is there a possibility to add this feature somehow? I’m working on a modern Rails equivalent for Next.js, and like a Rails controller, I want a single file for handling all my CRUD operations. I’d really like to avoid cluttering the file system with a bunch of re-export files. This type of REST API is so common, it seems like a great feature candidate.
Something else I intuitively tried was:
Yeah agreed that’s probably the best way 👍
@Timer What should be the value of the
worldparameter when I access the route/hello?undefinedor[]?I assume this syntax would also work for
pages/hello/[[world]].js? What would be the value ofworldin this case?undefinedor''?Personally I lean towards
[]and''.Optional route params, the thing that is asked for in the feature request.
@flybayer @yanv1991 if it’s helpful, as a workaround I’m using the experimental rewrittes in
next.config.jsOf course, not quite the same. It’ll have
/indexas pathname instead of/and slug will be["index"]. Also, usingnext/linkis tricky since it isn’t aware of this rewrite 🤷♀For now, it’s okay-ish (at least not two lambdas generated) but I’m also waiting to see any progress on this, so I can remove this rewrite and the ugly thing I have to make
next/linkwork with this setup when it needs to go to the root path (with pre-fetching thing).It originally tried to fix this issue, but turned into something completely different.
Also note that it links to a very specific comment, only talking about 404 vs 500 behavior.
This issue is about asking that index routes 200.
@Vadorequest this was fixed by https://github.com/vercel/next.js/pull/12887, not https://github.com/vercel/next.js/pull/10502.
https://github.com/vercel/next.js/pull/10502 is a totally unrelated PR!
FYI this seems to have been fixed by https://github.com/vercel/next.js/pull/10502 and released 8 days ago in v9.4.2
https://github.com/vercel/next.js/releases/tag/v9.4.2
👍
@Timer What’s the behavior of
Would it error?
@laleksiunas I just started setting up some tests for it, but I’m not sure yet whether I’m going to finish this, so you can take it if you want.
We run experimental options against nextjs.org, vercel.com etc to test them against production workloads. I’d hardly call new features “ugly hacks”, it’s a new feature being adopted in projects we control.
~can’t be used as a filename character btw, it won’t work on all platforms.Something we’ve talked about it having
[[optionalparams]]syntax for optional routes.From the Dynamic Routing docs:
It seems they use the experimental re-routing feature themselves.
My 2 cents: something like
~path[...asQuery].js. Example:The following would match:
This allows to easily see the difference between catch-all-paths routes and traditional folder-based routing, and allow customizing the name of the
router.queryvariable. A default value for the queries could beindex, or perhaps simplyundefined.What about this in the
pages/index.jsfile?I haven’t tested it yet, but it’s used in this example
@timneutkens is this feature going to be included soon? I have another problem having an
index.jsthat exports the[...slug].jsto catch the root index, it’s actually creating 2 separated lambdas.And it seems like, even if I try to add a
It makes
GET /pages/api/userswork, butGET /pages/api/users/gives me a 500. Without the index file that route 404s.