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.

CanDeactivate guard is triggered again by its own UrlTree return

See original GitHub issue

🐞 bug report

Affected Package

The issue is caused by package @angular/router

Is this a regression?

Unclear, we didn't use this feature in previous angular versions.

Description

When returning a UrlTree from a CanDeactivate guard, the same guard is executed again

πŸ”¬ Minimal Reproduction

https://stackblitz.com/edit/angular-8-candeactivate-guard-error-infinite-loop

Steps for reproduction:

  • Ignore anything in the UI
  • Click on the Go to login-2 anchor
  • Infinite navigation loop is triggered

πŸ”₯ Exception or Error

∞ loop

🌍 Your Environment

Angular Version:


8.0.0

Anything else relevant?

In my opinion returned UrlTree should not trigger again the guard returning it, otherwise that will force to keep logic (or even state) into the guard to control when to return UrlTree, which might lead into coupling.

Apologies if this is the desired behavior, if that is the case maybe the documentation should state that.

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Reactions:4
  • Comments:12 (5 by maintainers)

github_iconTop GitHub Comments

1reaction
angular-robot[bot]commented, Jun 4, 2021

Just a heads up that we kicked off a community voting process for your feature request. There are 20 days until the voting process ends.

Find more details about Angular’s feature request process in our documentation.

1reaction
jvquarckcommented, Oct 22, 2019

I don’t think that the documentation clearly states that returning a urlTree might retrigger this guard and possibly create an infinite loop. This usage case of the canDeactivate guard is like an interceptor. You are modifying this request/navigation based on certain conditions. If we translate this current behavior to interceptors it would mean that you’d need to keep state in every interceptor in the stack, since every interception would trigger again the entire interceptor stack and you’d need to check if you have modified the request already or not.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Angular CanDeactivate Guard is triggering recursively and ...
To achieve the above am returning a Subject in canDeactivate() present in ProductsComponent. when user confirms to leave am emitting UrlTree(Β ...
Read more >
CanDeactivate - Angular
If any guard returns a UrlTree , current navigation is cancelled and a new navigation begins to the UrlTree returned from the guard....
Read more >
Angular Deep Dive into CanDeactivate Route Guard
This guard will be used by the router to decide if the route can be deactivated. It can be implemented in any Angular...
Read more >
@angular/router@15.0.4 - jsDocs.io
An event triggered at the end of the Guard phase of routing. ... From the perspective of the router, the router never "goes...
Read more >
Router Tutorial: Tour of Heroes - Angular 10 - W3cubDocs
Click the "Back" button and the app returns to the heroes list which displays the changed hero ... Behind this behavior is the...
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