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
._matchedRoute[Name] inconsistency with nested routes. #478
Comments
It looks like |
As a workaround, change this line:
to
|
@jcao219 That workaround doesn't work unfortunately. |
@mirague That's unfortunate. IIRC The root cause of this issue is koa-router currently uses simply the last matching route as _matchedRoute. When you have multiple routes that match, it does not have a good way to figure out which route you consider the "matchedRoute". I don't know of a perfect solution to this, and although I did submit a best-effort PR #492 to fix it, that solution has problems of its own. One possible option is to return an array of matchedRoutes and let the user decide which to use |
There seems to be an issue with nested routes and the values of matchedRoute and matchedRouteName. Using one router directly work as expected, but for nested routes added and controlled like below, then the values seem to stop at the parent and disregard the child.
node.js: 10.6.0
npm: 6.4.1
koa-router: 7.4.0
koa: 2.5.2
Code sample:
Expected Behavior:
ctx.matchedRoute
=/child/test/:demo
ctx.matchedRouteName
=c_test
Actual Behavior:
ctx.matchedRoute
=/child
ctx.matchedRouteName
=undefined
Fun fact
You get a different result again if you initialize the router with a prefix
Yielding
ctx.matchedRoute
to be(.*)
. Not my use case scenario here, but tested it to see if it solved my problem, it did not, and unexpectedly, gave a completely different result.The text was updated successfully, but these errors were encountered: