Skip to content

setUpLocationSync causes an duplicate navigation events #21610

Closed

Description

@bourey

Angular's router upgrade code provides a setUpLocationSync method which listens for AngularJS-initiated $locationChangeStart events, then calls Angular's router.navigateByUrl. The docs for this method state that "History.pushState does not fire onPopState, so the Angular location doesn't detect it. The workaround is to attach a location change listener"(

* History.pushState does not fire onPopState, so the Angular location
). Without this code, the Angular router does not respond to location changes initiated by AngularJS, regardless of whether the pages themselves are associated w/ AngularJS or Angular content. For example, AngularJS-based navigation links would fail to trigger a routing event between two Angular pages.

While this code works for AngularJS-initiated location changes, it currently produces duplicate NavigationStart, etc. events for Angular-initiated changes. The AngularJS router sees that the browser location has changed and triggers a location change event, which in turn triggers an additional NavigationStart event in Angular. It appears that we need some way to trigger Angular navigation only for location changes that originate in AngularJS.

This issue is currently causing duplicate router events for any Angular-driven page transition in dual-router apps, which is a performance issue.

Activity

  1. added
    regression
    on Jan 19, 2018
  2. aciccarello commented on Jan 23, 2018

    @aciccarello
    Contributor

    I believe I am encountering a related issue where my call to router.navigate() with query params within the ngOnInit() method is being overriden because the $locationChangeStart event is triggering a route back to the same route with no queryParams.

    My current workaround is to use setTimeout(fn, 0) to delay the router.navigate() call till after $locationChangeStart.

  3. gkalpak commented on Jan 23, 2018

    @gkalpak
    Member

    It is hard to investigate without a reproduction. @bourey, could you create a (preferably minimal 😁) reproduction; either as a repo or a live demo?
    In case it helps, here are my stackblitz "playgrounds": ngUpgrade-static, ngUpgrade-lite

    I wonder if this is related to #20244 at all.

    @jasonaden, you marked it as a regression. Is there any previous version that is known to work?

  4. aciccarello commented on Jan 23, 2018

    @aciccarello
    Contributor

    @gkalpak It took me about 2 hours to get a concise reproduction but you can see the issue that I'm experiencing in this stackblitz. My example is complicated by some special bootstrapping but I believe the issue should be clearly demonstrated.

    https://stackblitz.com/edit/ajc-angular-upgrade-locationsync-bug

    I also tested my stackblitz with Angular versions 4.4.6 and 4.0.3 and the issue remained.

  5. added this to the Backlog milestone on Jan 23, 2018
  6. jasonaden commented on Jan 29, 2018

    @jasonaden
    Contributor

    @gkalpak We thought it was a regression, but as it turns out it had previously been broken. Changing the label.

  7. added and removed
    regression
    on Jan 29, 2018
  8. jasonaden commented on Jan 29, 2018

    @jasonaden
    Contributor

    Also, this fix (#21728) went in so we can make a change such as this to upgrade.ts:

    ngUpgrade.$injector.get('$rootScope')
      .$on('$locationChangeSuccess', (_: any, next: string, __: string, state: any) => {
        const url = document.createElement('a');
        url.href = next;
    
        if (!state || !state.navigationId) {
          router.navigateByUrl(url.pathname + url.search + url.hash, {skipLocationChange: true});
        }
    });
    

    We are currently testing this change internally to make sure it doesn't open up other issues.

  9. modified the milestones: Backlog, needsTriage on Feb 26, 2018
  10. 11 remaining items

  11. added 6 commits that reference this issue on Oct 14, 2021
    5fd822d
  12. added a commit that references this issue on Feb 2, 2022
    b9aab0c
  13. angular-automatic-lock-bot commented on Mar 5, 2022

    @angular-automatic-lock-bot

    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.

  14. locked and limited conversation to collaborators on Mar 5, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

P3area: routerfreq3: highhotlist: googlestate: confirmedtype: bug/fix

Type

No type

Projects

No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions