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

Child Route activate() called before childRouter has refreshed baseUrl #378

Closed
tomtomau opened this issue Jul 27, 2016 · 6 comments
Closed
Assignees

Comments

@tomtomau
Copy link

I'm submitting a bug report

  • Library Version:
    1.0.0-rc1.0.5

Current behavior:

Originally in Gitter chat:

So I have a AppRouter (A) which has a Route (B) which is a ChildRouter under the likes of "/register", I want to dynamically throw the user around different routes in this child router. So I started by creating essentially an index route (C) for ChildRouter (B) (i.e. it handles the "") request. What I would ideally do, is, in the activate() method of the index route (C), return a promise which is a redirect to a different route (D) (which might be like "/step-1", so in sum it should be "/register/step-1"),as a route that belongs to ChildRouter (B). Catch is, if I try to router.navigateToRoute("route-d") sort of thing in the activate method of C, then it trys to redirect to the absolute app root ("/step-1" and not "/register/step-1"). If I call the same navigateToRoute method but later as part of click.delegate() call, it works perfectly. I cannot work out why it doesn't work in the activate method?

We established my approach was largely flawed anyway for the use case, but I can at least answer why I had this issue.

After debugging, I tracked it down to the activate() method of a child VM (C) of the child router (B) is being called before "refreshBaseUrl()" is called. Calling refreshBaseUrl() at the start of generate for example then breaks because the parent (AppRouter) is currently navigating and doesn't have a currentInstruction of it landing on the childRouter.

It might be a bit of a failing of my understanding of the router in that, I would have thought at the point that we were activate()'ing a one of the childRouter's child routes, that the AppRouter would have had a currentInstruction (ie, it landed on the childRouter).

Regardless, the behaviour as described below is not documented anywhere at present.

"During the activation of a VM belonging to a route within a ChildRouter, redirectToRoute on the child router will not generate the correct URL as the AppRouter has not resolved for the currentInstruction"

Happy to discuss further, would love to give an example but a bit time crunched at present but will aim to do so.

@EisenbergEffect
Copy link
Contributor

The only way to redirect during a navigation is to return a navigation comment (such as Redirect or RedirectToRouter) from the canActivate callback. Trying to do it later on or via a navigation api call will break things. Would you be interested in contributing by submitting some explanation to our documentation?

@vuorinem
Copy link
Contributor

@EisenbergEffect This doesn't seem to be working with child routes.

I created a gist: https://gist.run/?id=1bca105623d3c88400738985d008fc2e
Clicking "Go" navigates to route1/child1, and canActivate in child1 returns new RouteRedirect('child2'). This throws an error Route not found: /child2.

Have I misunderstood something, or is this a bug in the router?

@Alexander-Taran
Copy link
Contributor

Unanswered question.
Does it still need a paragraph documentation?

@davismj
Copy link
Member

davismj commented Mar 21, 2018

I'm thinking this has been fixed by 1432234. Please reoopen if not.

@davismj davismj closed this as completed Mar 21, 2018
@vuorinem
Copy link
Contributor

Looks like I still have the same issue. I seem to have overwritten the GistRun above, but the code from #306 still behaves the same with router 1.5.0.

Original gist: https://gist.run/?id=8a73eca78d76065306dd459b64929ceb
Repro with aurelia-router 1.5.0: https://github.com/vuorinem/aurelia-router-bug

activate() {
  this.secondInActivate = this.router.generate("second");
}
attached() {
  this.secondInAttached = this.router.generate("second");
}

The call from activate() will generate the url #/second, the one from attached() generates #/child/second.

@davismj davismj self-assigned this Apr 8, 2018
@jamiepollard
Copy link

jamiepollard commented Apr 19, 2018

As above, seems this might be a regression, we're getting the same issue again with router 1.5.0 (was fixed previously)

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

No branches or pull requests

6 participants