Skip to content

fix(router): Only trigger router navigation on popstate events from Location subscription - #43328

Closed
atscott wants to merge 1 commit into
angular:masterfrom
atscott:simplifyRouterLocationListener
Closed

atscott wants to merge 1 commit into
angular:masterfrom
atscott:simplifyRouterLocationListener

Conversation

@atscott

@atscott atscott commented Sep 2, 2021 •

Copy link
Copy Markdown
Contributor

With the merging of #41730, our tests now more closely emulate real
browser behavior. This means that we can make changes to the Router which depend
on actual browser behavior rather than incorrectly mocked test behavior.

This change updates the logic in the Router to only trigger
navigations on popstate events. Since our SpyLocation now triggers
the popstate event correctly on simulateHashChange, back,
forward, and simulatePopState, we are able to rely on this being
true for all of these navigations in tests when using SpyLocation (the
default for RouterTestingModule).

In addition, this behavior relies on documented browser behavior: The
popstate event happens due to browser transitions as a result of user
triggered "back" button or otherwise.
https://developer.mozilla.org/en-US/docs/Web/API/Window/popstate_event#when_popstate_is_sent

In addition, this change chooses popstate over hashchange because it
is the event that is already being used for navigations since the
documented behavior of browsers is that popstate happens before
hashchange.

fixes #41782

reviewer note: this is marked as major because #41730 was merged as a breaking change to the major branch.

green global presubmit train

… `Location` subscription

With the merging of angular#41730, our tests now more closely emulate real
browser behavior. This means that we can make changes to the `Router` which depend
on actual browser behavior rather than incorrectly mocked test behavior.

This change updates the logic in the `Router` to only trigger
navigations on `popstate` events. Since our `SpyLocation` now triggers
the `popstate` event correctly on `simulateHashChange`, `back`,
`forward`, and `simulatePopState`, we are able to rely on this being
true for all of these navigations in tests when using `SpyLocation` (the
default for `RouterTestingModule`).

In addition, this behavior relies on documented browser behavior: The
`popstate` event happens due to browser transitions as a result of user
triggered "back" button or otherwise.
https://developer.mozilla.org/en-US/docs/Web/API/Window/popstate_event#when_popstate_is_sent

In addition, this change chooses `popstate` over `hashchange` because it
is the event that is already being used for navigations since the
documented behavior of browsers is that `popstate` happens before
`hashchange`.

fixes angular#41782
@atscott atscott added area: router target: major This PR is targeted for the next major release labels Sep 2, 2021
@ngbot ngbot Bot modified the milestone: Backlog Sep 2, 2021
@google-cla google-cla Bot added the cla: yes label Sep 2, 2021
@atscott
atscott marked this pull request as ready for review September 3, 2021 16:46

@AndrewKushnir AndrewKushnir left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM (just one tiny comment to see if we can add more docs) 👍

Comment thread packages/router/src/router.ts
@atscott atscott added action: merge The PR is ready for merge by the caretaker merge: caretaker note Alert the caretaker performing the merge to check the PR for an out of normal action needed or note and removed merge: caretaker note Alert the caretaker performing the merge to check the PR for an out of normal action needed or note labels Sep 8, 2021
@atscott atscott closed this in 9e039ca Sep 8, 2021
@angular-automatic-lock-bot

Copy link
Copy Markdown

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.

@angular-automatic-lock-bot angular-automatic-lock-bot Bot locked and limited conversation to collaborators Oct 9, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

action: merge The PR is ready for merge by the caretaker area: router cla: yes target: major This PR is targeted for the next major release

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Changing the URI in the browser directly will fire guards and resolvers of the route TWICE

2 participants