Skip to content
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

Open
ghost opened this issue Sep 5, 2018 · 4 comments
Open

._matchedRoute[Name] inconsistency with nested routes. #478

ghost opened this issue Sep 5, 2018 · 4 comments

Comments

@ghost
Copy link

ghost commented Sep 5, 2018

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:

var routes = new Router();
var child = new Router();

routes.get(
  'index',
  '/',
  app.context.routes.backend.core.index()
);

child.get(
  'c_test',
  '/test/:demo',
  app.context.routes.backend.core.index()
);

routes.use( '/child', child.routes(), child.allowedMethods() );

app.use( routes.routes() );
app.use( routes.allowedMethods() );

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

var child = new Router({
 prefix: 'child'
});

routes.use( '', child.routes(), child.allowedMethods() );

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.

@etaoins
Copy link

etaoins commented Sep 10, 2018

It looks like ctx.routerName will contain the name of the nested child route

@jcao219
Copy link

jcao219 commented Jan 12, 2019

As a workaround, change this line:

routes.use( '/child', child.routes(), child.allowedMethods() );

to

routes.use( '/child', child.allowedMethods(), child.routes() );

@mirague
Copy link

mirague commented Jan 29, 2019

@jcao219 That workaround doesn't work unfortunately.

@jcao219
Copy link

jcao219 commented Jan 29, 2019

@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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants