-
Notifications
You must be signed in to change notification settings - Fork 27k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Catch-all routes do not catch the root index route #10488
Comments
And it seems like, even if I try to add a // /api/users/index.js
export { default } from "./[...slug]"; It makes |
With regards to the initial issue, this is expected behavior, catch-all means the segment you define is a catch-all. What you're referring is an optional segment where |
@timneutkens I just tried it in a codesandbox but I'll see if I can PR something tonight or tomorrow. |
@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:
|
I'll keep the issue open to see if there's anyone else asking for it. |
@timneutkens created #10502 Been thinking a bit more about this issue and I think the requested behavior could make more sense for the following reasons:
Current situation, imagine we have a catch-all route like: // /api/posts/[...slug].js
export default function (req, res) {
res.send(getPost(req.query.slug))
} And we fetch the following urls:
Imagine we add a index route: // /api/posts/index.js
export { default } from './[...slug]'; now this behavior becomes
Now, imagine // /api/posts/[...slug].js
export default function (req, res) {
res.send(getPost(req.query.slug))
} the behavior would be more predictable and uniform and zen
|
@Janpot, Yes!! you very eloquently described how I'm thinking about this. |
+1 having this same issue as well as the same 500 issue when doing Having this setup:
It doesn't match exportPathMap: async function (defaultPathMap) {
return merge({}, defaultPathMap, {
'/': { page: '/[...fragment]', query: { fragment: ['/'] } },
})
}, So this correctly returns as @Janpot described on last table. However, I'm deploying to Zeit Now and I couldn't figure how to make that work since Any update on this? or does anyone have a better workaround for the time being to make this work with |
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 Currently:
After this feature:
|
@timneutkens is this feature going to be included soon? I have another problem having an |
@flybayer @yanv1991 if it's helpful, as a workaround I'm using the experimental rewrittes in modules.export = {
experimental: {
rewrites: async () => [
{ source: '/', destination: '/index?slug=index&slug' }
]
}
} Of course, not quite the same. It'll have 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 |
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 EDIT: import Page from './[...slug]'
export default Page This works with |
@programbo the problem with that approach is because creates 2 lambdas one for [...slug] and one for index.tsx |
I came here because I ran into the same issue, expecting the Catch all route to catch the root as well. The only problem I see is that people out there might by now be relying on the current behavior, so if we could handle this in a backward-compatible way, that would be amazin. |
What about this in the export { default, getStaticProps, getStaticPaths } from './[...slug]' I haven't tested it yet, but it's used in this example |
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 |
fwiw I had a client ask me about this the other day. Same approach of doing // pages/hello/[...world].js
export default function World() {
return <div>...</div>
}
// Do what you need to fetch data // pages/hello/index.js
import Index from './[world]'
export default Index
// Do what you need to fetch data, but with hardcoded value |
I'd also like this! For deployments, I've just been redirects for anybody that goes to In local dev it's a bit of a chore to have to remember to add the page number for every route. |
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. |
From the Dynamic Routing docs:
It seems they use the experimental re-routing feature themselves.
My 2 cents: something like
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 |
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.
Something we've talked about it having |
Apologies, I realize this may have come off more aggressive than I intended it to be. What I meant is that it's undocumented and not production-ready for teams outside Vercel.
Could you clarify what you mean by "optional routes"? |
Optional route params, the thing that is asked for in the feature request. |
Concretely, making a page named: Today, making a page named: We're happy to take a PR if anyone wants to work on this. |
@Timer What should be the value of the I assume this syntax would also work for Personally I lean towards |
Yeah agreed that's probably the best way 👍 |
pages/hello/[[...world]].js looks nice! I can start working on this next week, please let me know if someone begins working sooner. |
For We should also throw a build error if you define both Please do not PR support for |
@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. |
👍 |
@Janpot Well, I wouldn't start working this week, either way. Just ping me if you see that you will not have time to finish it up and I can continue your work. |
…sh (#10502) Failing test case for #10488 (comment) This used to give a 500 in dev environment
…sh (vercel#10502) Failing test case for vercel#10488 (comment) This used to give a 500 in dev environment
FYI this seems to have been fixed by #10502 and released 8 days ago in v9.4.2 |
@Vadorequest this was fixed by #12887, not #10502. #10502 is a totally unrelated PR! |
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. |
…sh (vercel#10502) Failing test case for vercel#10488 (comment) This used to give a 500 in dev environment
clean solution! |
Is it possible that this doesn't affect plain serverless node functions (decoupled from next.js)? Working: ✅ optional catch all example from https://github.com/storyofams/next-api-decorators/tree/master/examples/route-matching |
This issue has been automatically locked due to no recent activity. If you are running into a similar issue, please create a new issue with the steps to reproduce. Thank you. |
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:
After reading the docs, I was sure I could catch all of those using this:
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:
Catches all of these:
Screenshots
N/A
System information
N/A
Additional context
Next.js 9.2.2-canary.15
The text was updated successfully, but these errors were encountered: