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
Navigation events are called twice when using HashLocationStrategy #16710
Comments
No activity or solution in months? This bug still exists. @jasonhodges Is this still on Angulars radar? Its a significant bug, in my opinion. Duplicate issue at #18366 |
Getting five NavigationEnd events fired when router.events.subscribe((val) => {
if (val instanceof NavigationEnd) {
console.log('NavigationEnd!');
}
}); |
have same problem. |
Have the same problem. Use Angular version: 5.2.2 and Typescript 2.6.2 |
Same here with Angular 5. |
I have the same problem. Angular version 5.2.9 |
is there any update ?? |
I am able to work around this issue but I am also very interested in a fix. |
@jfox459 can u please provide your work around ? |
this worked for me
|
This bug still exists with v6. I'm not using any The |
I have the same issue in chrome but not in firefox, canActivate guard called twice, any solution ? :( |
i have the same issue as @metaloha described In my case it is critical issue due to
so this behaviour brokes my logic for now i use workaround and call router.navigate in setTimeout() callback but it seem not normal behaviour |
same use case as @rodmax, did you find a workaround without having to resort to settimeout ? also if the timeout is short I am still seeing the issue so redirections are slow now. |
We ran into same issue, we have a complex navigation logic with multiple options for redirection depending on conditions. And if something we try to navigate to does not exist then modal window is shown. But because of this hashChange it runs guard twice and therefore we get 2 error modals which is not nice, I still haven't found a way to work it around. EDIT: wrapping imperative navigation call into setTImeout helped, but it's not really that nice, as I also had to wrap setTimeout in a promise. |
Hi, any updates here? We have the same issue. Thanks! |
Any update with this issue? Thanks! |
Hi there, here is our case example:
|
Issue still exists. |
Fix issue 16710 (angular#16710)
Fix issue 16710. Navigation events are called twice when using HashLocationStrategy. When user change url manually in browser URL. ex: Router A(component A) using navigateByUrl(in guard) to redirect to Router B(Component B). When user enter router A in browser URL. That follow will be call twice. Detail in issue 16710 angular#16710
Yep, that's it. Not sure what happened there. So I think I've pinpointed the issue. It's related to this bit of code in the angular/packages/router/src/router.ts Lines 1147 to 1151 in bd7f440
However, because the duplicate navigations are scheduled in a angular/packages/router/src/router.ts Lines 867 to 869 in bd7f440
the first one executes and calls router.navigate from the guard before the second, duplicate navigation from hashchange is processed. This means the check above won't work, because another navigation got inserted between the two duplicates.
This could potentially be solved by adding similar double event guarding logic to the location subscription and do the check before scheduling the navigation. The check would have to be modified a bit (probably with a |
Awesome to see an old issue acknowledge and found. Thanks for investigating. |
@atscott I dont have time to rewrite a test case as your comment but you can refer my code to resolve this problem |
…correctly The current method of handling duplicate navigations caused by 'hashchange' and 'popstate' events for the same url change does not correctly handle cancelled navigations. Because `scheduleNavigation` is called in a `setTimeout` in the location change subscription, the duplicate navigations are not flushed at the same time. This means that if the initial navigation hits a guard that schedules a new navigation, the navigation for the duplicate event will not compare to the correct transition (because we inserted another navigation between the duplicates). See angular#16710 (comment) Fixes angular#16710
…correctly The current method of handling duplicate navigations caused by 'hashchange' and 'popstate' events for the same url change does not correctly handle cancelled navigations. Because `scheduleNavigation` is called in a `setTimeout` in the location change subscription, the duplicate navigations are not flushed at the same time. This means that if the initial navigation hits a guard that schedules a new navigation, the navigation for the duplicate event will not compare to the correct transition (because we inserted another navigation between the duplicates). See angular#16710 (comment) Fixes angular#16710
…correctly The current method of handling duplicate navigations caused by 'hashchange' and 'popstate' events for the same url change does not correctly handle cancelled navigations. Because `scheduleNavigation` is called in a `setTimeout` in the location change subscription, the duplicate navigations are not flushed at the same time. This means that if the initial navigation hits a guard that schedules a new navigation, the navigation for the duplicate event will not compare to the correct transition (because we inserted another navigation between the duplicates). See angular#16710 (comment) Fixes angular#16710
…correctly The current method of handling duplicate navigations caused by 'hashchange' and 'popstate' events for the same url change does not correctly handle cancelled navigations. Because `scheduleNavigation` is called in a `setTimeout` in the location change subscription, the duplicate navigations are not flushed at the same time. This means that if the initial navigation hits a guard that schedules a new navigation, the navigation for the duplicate event will not compare to the correct transition (because we inserted another navigation between the duplicates). See angular#16710 (comment) Fixes angular#16710
…correctly The current method of handling duplicate navigations caused by 'hashchange' and 'popstate' events for the same url change does not correctly handle cancelled navigations. Because `scheduleNavigation` is called in a `setTimeout` in the location change subscription, the duplicate navigations are not flushed at the same time. This means that if the initial navigation hits a guard that schedules a new navigation, the navigation for the duplicate event will not compare to the correct transition (because we inserted another navigation between the duplicates). See angular#16710 (comment) Fixes angular#16710
This PR changes the logic for determining when to skip route processing from using the URL of the last attempted navigation to the actual resulting URL after that transition. Because guards may prevent navigation and reset the browser URL, the raw URL of the previous transition may not match the actual URL of the browser at the end of the navigation process. For that reason, we need to use `urlAfterRedirects` instead. Other notes: These checks in scheduleNavigation were added in angular@eb2ceff The test still passes and, more surprisingly, passes if the checks are removed completely. There have likely been changes to the navigation handling that handle the test in a different way. That said, it still appears to be important to keep the checks there in some capacity because it does affect how many navigation events occur. This addresses an issue that came up in angular#16710: angular#16710 (comment) This also partially addresses angular#13586 in fixing history for imperative navigations that are cancelled by guards.
This PR changes the logic for determining when to skip route processing from using the URL of the last attempted navigation to the actual resulting URL after that transition. Because guards may prevent navigation and reset the browser URL, the raw URL of the previous transition may not match the actual URL of the browser at the end of the navigation process. For that reason, we need to use `urlAfterRedirects` instead. Other notes: These checks in scheduleNavigation were added in angular@eb2ceff The test still passes and, more surprisingly, passes if the checks are removed completely. There have likely been changes to the navigation handling that handle the test in a different way. That said, it still appears to be important to keep the checks there in some capacity because it does affect how many navigation events occur. This addresses an issue that came up in angular#16710: angular#16710 (comment) This also partially addresses angular#13586 in fixing history for imperative navigations that are cancelled by guards.
…url (angular#37408) This PR changes the logic for determining when to skip route processing from using the URL of the last attempted navigation to the actual resulting URL after that transition. Because guards may prevent navigation and reset the browser URL, the raw URL of the previous transition may not match the actual URL of the browser at the end of the navigation process. For that reason, we need to use `urlAfterRedirects` instead. Other notes: These checks in scheduleNavigation were added in angular@eb2ceff The test still passes and, more surprisingly, passes if the checks are removed completely. There have likely been changes to the navigation handling that handle the test in a different way. That said, it still appears to be important to keep the checks there in some capacity because it does affect how many navigation events occur. This addresses an issue that came up in angular#16710: angular#16710 (comment) This also partially addresses angular#13586 in fixing history for imperative navigations that are cancelled by guards. PR Close angular#37408
…correctly The current method of handling duplicate navigations caused by 'hashchange' and 'popstate' events for the same url change does not correctly handle cancelled navigations. Because `scheduleNavigation` is called in a `setTimeout` in the location change subscription, the duplicate navigations are not flushed at the same time. This means that if the initial navigation hits a guard that schedules a new navigation, the navigation for the duplicate event will not compare to the correct transition (because we inserted another navigation between the duplicates). See angular#16710 (comment) Fixes angular#16710
…correctly The current method of handling duplicate navigations caused by 'hashchange' and 'popstate' events for the same url change does not correctly handle cancelled navigations. Because `scheduleNavigation` is called in a `setTimeout` in the location change subscription, the duplicate navigations are not flushed at the same time. This means that if the initial navigation hits a guard that schedules a new navigation, the navigation for the duplicate event will not compare to the correct transition (because we inserted another navigation between the duplicates). See angular#16710 (comment) Fixes angular#16710
…correctly The current method of handling duplicate navigations caused by 'hashchange' and 'popstate' events for the same url change does not correctly handle cancelled navigations. Because `scheduleNavigation` is called in a `setTimeout` in the location change subscription, the duplicate navigations are not flushed at the same time. This means that if the initial navigation hits a guard that schedules a new navigation, the navigation for the duplicate event will not compare to the correct transition (because we inserted another navigation between the duplicates). See angular#16710 (comment) Fixes angular#16710
…correctly The current method of handling duplicate navigations caused by 'hashchange' and 'popstate' events for the same url change does not correctly handle cancelled navigations. Because `scheduleNavigation` is called in a `setTimeout` in the location change subscription, the duplicate navigations are not flushed at the same time. This means that if the initial navigation hits a guard that schedules a new navigation, the navigation for the duplicate event will not compare to the correct transition (because we inserted another navigation between the duplicates). See angular#16710 (comment) Fixes angular#16710
…correctly The current method of handling duplicate navigations caused by 'hashchange' and 'popstate' events for the same url change does not correctly handle cancelled navigations. Because `scheduleNavigation` is called in a `setTimeout` in the location change subscription, the duplicate navigations are not flushed at the same time. This means that if the initial navigation hits a guard that schedules a new navigation, the navigation for the duplicate event will not compare to the correct transition (because we inserted another navigation between the duplicates). See angular#16710 (comment) Fixes angular#16710
…correctly The current method of handling duplicate navigations caused by 'hashchange' and 'popstate' events for the same url change does not correctly handle cancelled navigations. Because `scheduleNavigation` is called in a `setTimeout` in the location change subscription, the duplicate navigations are not flushed at the same time. This means that if the initial navigation hits a guard that schedules a new navigation, the navigation for the duplicate event will not compare to the correct transition (because we inserted another navigation between the duplicates). See angular#16710 (comment) Fixes angular#16710
…correctly The current method of handling duplicate navigations caused by 'hashchange' and 'popstate' events for the same url change does not correctly handle cancelled navigations. Because `scheduleNavigation` is called in a `setTimeout` in the location change subscription, the duplicate navigations are not flushed at the same time. This means that if the initial navigation hits a guard that schedules a new navigation, the navigation for the duplicate event will not compare to the correct transition (because we inserted another navigation between the duplicates). See angular#16710 (comment) Fixes angular#16710
This PR changes the logic for determining when to skip route processing from using the URL of the last attempted navigation to the actual resulting URL after that transition. Because guards may prevent navigation and reset the browser URL, the raw URL of the previous transition may not match the actual URL of the browser at the end of the navigation process. For that reason, we need to use `urlAfterRedirects` instead. Other notes: These checks in scheduleNavigation were added in angular@eb2ceff The test still passes and, more surprisingly, passes if the checks are removed completely. There have likely been changes to the navigation handling that handle the test in a different way. That said, it still appears to be important to keep the checks there in some capacity because it does affect how many navigation events occur. This addresses an issue that came up in angular#16710: angular#16710 (comment) This also partially addresses angular#13586 in fixing history for imperative navigations that are cancelled by guards.
This PR changes the logic for determining when to skip route processing from using the URL of the last attempted navigation to the actual resulting URL after that transition. Because guards may prevent navigation and reset the browser URL, the raw URL of the previous transition may not match the actual URL of the browser at the end of the navigation process. For that reason, we need to use `urlAfterRedirects` instead. Other notes: These checks in scheduleNavigation were added in angular@eb2ceff The test still passes and, more surprisingly, passes if the checks are removed completely. There have likely been changes to the navigation handling that handle the test in a different way. That said, it still appears to be important to keep the checks there in some capacity because it does affect how many navigation events occur. This addresses an issue that came up in angular#16710: angular#16710 (comment) This also partially addresses angular#13586 in fixing history for imperative navigations that are cancelled by guards.
…url (#37716) This PR changes the logic for determining when to skip route processing from using the URL of the last attempted navigation to the actual resulting URL after that transition. Because guards may prevent navigation and reset the browser URL, the raw URL of the previous transition may not match the actual URL of the browser at the end of the navigation process. For that reason, we need to use `urlAfterRedirects` instead. Other notes: These checks in scheduleNavigation were added in eb2ceff The test still passes and, more surprisingly, passes if the checks are removed completely. There have likely been changes to the navigation handling that handle the test in a different way. That said, it still appears to be important to keep the checks there in some capacity because it does affect how many navigation events occur. This addresses an issue that came up in #16710: #16710 (comment) This also partially addresses #13586 in fixing history for imperative navigations that are cancelled by guards. PR Close #37716
…correctly The current method of handling duplicate navigations caused by 'hashchange' and 'popstate' events for the same url change does not correctly handle cancelled navigations. Because `scheduleNavigation` is called in a `setTimeout` in the location change subscription, the duplicate navigations are not flushed at the same time. This means that if the initial navigation hits a guard that schedules a new navigation, the navigation for the duplicate event will not compare to the correct transition (because we inserted another navigation between the duplicates). See angular#16710 (comment) Fixes angular#16710
…correctly The current method of handling duplicate navigations caused by 'hashchange' and 'popstate' events for the same url change does not correctly handle cancelled navigations. Because `scheduleNavigation` is called in a `setTimeout` in the location change subscription, the duplicate navigations are not flushed at the same time. This means that if the initial navigation hits a guard that schedules a new navigation, the navigation for the duplicate event will not compare to the correct transition (because we inserted another navigation between the duplicates). See angular#16710 (comment) Fixes angular#16710
…correctly The current method of handling duplicate navigations caused by 'hashchange' and 'popstate' events for the same url change does not correctly handle cancelled navigations. Because `scheduleNavigation` is called in a `setTimeout` in the location change subscription, the duplicate navigations are not flushed at the same time. This means that if the initial navigation hits a guard that schedules a new navigation, the navigation for the duplicate event will not compare to the correct transition (because we inserted another navigation between the duplicates). See angular#16710 (comment) Fixes angular#16710
This issue has been automatically locked due to inactivity. Read more about our automatic conversation locking policy. This action has been performed automatically by a bot. |
…correctly (angular#37674) The current method of handling duplicate navigations caused by 'hashchange' and 'popstate' events for the same url change does not correctly handle cancelled navigations. Because `scheduleNavigation` is called in a `setTimeout` in the location change subscription, the duplicate navigations are not flushed at the same time. This means that if the initial navigation hits a guard that schedules a new navigation, the navigation for the duplicate event will not compare to the correct transition (because we inserted another navigation between the duplicates). See angular#16710 (comment) Fixes angular#16710 PR Close angular#37674
I'm submitting a ... (check one with "x")
Current behavior
When using
HashLocationStrategy
and change url manually to the one, which is protected by CanActivate guard (this returns false and fire redirect to the other page), navigation events are called twice.Expected behavior
All navigation event are called once only.
Minimal reproduction of the problem with instructions
Use the demo in http://plnkr.co/edit/4xlYD9wmetG5G0Dvjp0E?p=preview
Open window mode http://run.plnkr.co/plunks/4xlYD9wmetG5G0Dvjp0E/#/component-one
Click
Guarded Component Two
link - only oneNavigationStart
andNavigationCancel
events logged in console.Manually change url from
/#/component-one
to/#/component-two
and hit enter - two pair ofNavigationStart
andNavigationCancel
events logged in console.What is the motivation / use case for changing the behavior?
As the duplication of navigation event causes guards logic to be executed twice (and this may contain API calls) - it is a performance issue. Potentially, if guard resolving depends on some conditions which are true after first failed attempt - the second one will be successful, however it is not desired behavior.
Please tell us about your environment:
Angular version: 4.1.0
Browser: Chrome 57
Language: TypeScript
The text was updated successfully, but these errors were encountered: