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/routerIs 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:
- Created 4 years ago
- Reactions:4
- Comments:12 (5 by maintainers)
Top 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 >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
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.
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 thecanDeactivate
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.