question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

bug canLoad WHEN redirect to ["/"] and return false

See original GitHub issue

I’m submitting a …

[x] bug report with CanLoad Guard

Current behavior Actually I lazy load my LoginModule and I have canLoad guard. In this canLoad I just check if token is valid.

  1. If NOT valid I simply return true; and it works LoginModule is allowed to load The problem is :
  2. if token IS valid I don’t want to load LoginModule I simply navigate to home route and return false using:
this.router.navigate(['/']);
return false;

Problem is when in canLoad guard you return false the url is automatically cleared and my navigate(['/']); is never fired :S

Expected behavior Simply execute navigate(['/']); when return false; in canLoad

Minimal reproduction of the problem with instructions can’t reproduce but here’s my routing:

const APP_ROUTES: Routes = [
  { path: 'login', loadChildren: 'app/login/login.module#LoginModule', canLoad: [CanLoadLoginGuard]},
  ...
  { path: '', component:HomeComponent, pathMatch: 'full', canActivate:[CanActivateHomePageGuard] },
  { path: '**', redirectTo:'' }
];

Please tell us about your environment: ng 4.0.3, ng-cli 1.0.0, ts 2.3 (win10,webstorm,npm)

Update (more infos)

I tried plunker here and can’t reproduce because I need to come for first time to certain route where I have canLoad, afaik in plunker preview you can’t come directly to …/mod1 but only on …/

I have zipped project with very light/simple example. So if you have angular-cli you can simply run npm i&& ng s and open your browser on localhost:4200/mod1 then CANCEL confirm() dialog, that’s all!

Download issue16197.zip project (angular-cli)

Cheers

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Reactions:3
  • Comments:8 (5 by maintainers)

github_iconTop GitHub Comments

5reactions
ghetolaycommented, Apr 21, 2017

Here is my understanding of this :

When a canLoad returns false the url is changed, not sure if it’s to the previous or if it’s the partial url of the route we’re trying to reach, but in this case if you try to go to /mod1 and canLoad return false the url is changed to /. It’s just the location url changing and not a navigate(). So then when the navigate(['/']) kicks in, the router thinks we’re moving from / to / and discard the navigation because it’s the same url.

Current workaround

You can create a route specific for that :

{
  path: 'redirectToRoot'
  redirectTo: '/'
}

And just navigate to it instead of navigating to root :

canLoad() {
  this.router.navigate(['redirectToRoot']);
  return false;
}
0reactions
angular-automatic-lock-bot[bot]commented, Sep 13, 2019

This issue has been automatically locked due to inactivity. Please file a new issue if you are encountering a similar or related problem.

Read more about our automatic conversation locking policy.

This action has been performed automatically by a bot.

Read more comments on GitHub >

github_iconTop Results From Across the Web

javascript - How to fix canLoad issue when we are returning ...
Try using catchError() operator which will handle your situation when error arrives instead of correct response. See example: canLoad(route: ...
Read more >
CanLoad - Angular
If all guards return true , navigation continues. If any guard returns false , navigation is cancelled. If any guard returns a UrlTree...
Read more >
How To Fix the Firefox "The Page Isn't Redirecting Properly ...
Seeing the "The Page Isn't Redirect Properly" error in Firefox? Learn what's going wrong and how you can fix the problem for good....
Read more >
How to Fix “Too Many Redirects” WordPress Error - DreamHost
The “too many redirects” WordPress error can occur when your site gets stuck in a redirection loop due to an issue with your...
Read more >
Better Redirects in Angular Route Guards | juri.dev
authService.isLoggedIn) { // redirect to some view explaining what happened this.router.navigateByUrl('/notauthorized'); return false; } ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found