refactor(router): Remove unnecessary setTimeout in UrlTree redirects#45735
Closed
atscott wants to merge 1 commit intoangular:masterfrom
Closed
refactor(router): Remove unnecessary setTimeout in UrlTree redirects#45735atscott wants to merge 1 commit intoangular:masterfrom
atscott wants to merge 1 commit intoangular:masterfrom
Conversation
Using `setTimeout` in the Router navigation pipeline creates fragile and unpredictable behavior. Additionally, it creates a new macrotask, which would trigger an unnecessary change detection in the application. This `setTimeout` was added in angular@15e3978. Both tests added in that commit still pass. Additionally, the comment for _why_ the `setTimeout` was added doesn't really explain how the described bug would occur. There has been a lot of work in the Router since then to stabalize edge case scenarios so it's possible it existed before but doesn't anymore. Removing this `setTimeout` revealed tests that relied on the navigation not completing. For example, the test suite did not have a route which matched the redirect, but the test passed because it ended before the redirect was flushed, so the `Router` never threw an error. Similar situations exist for the other use of `setTimeout` in the Route (the one in the location change listener). There were no other failures in TGP other than incorrectly written tests. BREAKING CHANGE: When a guard returns a `UrlTree`, the router would previously schedule the redirect navigation within a `setTimeout`. This timeout is now removed, which can result in test failures due to incorrectly written tests. Tests which perform navigations should ensure that all timeouts are flushed before making assertions. Tests should ensure they are capable of handling all redirects from the original navigation.
0e06921 to
b7756cb
Compare
Contributor
Author
|
TGP |
AndrewKushnir
approved these changes
Apr 27, 2022
Contributor
AndrewKushnir
left a comment
There was a problem hiding this comment.
This is awesome! 🎉 Thanks @atscott!
Contributor
Author
|
This PR was merged into the repository by commit 7b367d9. |
|
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. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to subscribe to this conversation on GitHub.
Already have an account?
Sign in.
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.

Using
setTimeoutin the Router navigation pipeline creates fragile andunpredictable behavior. Additionally, it creates a new macrotask, which
would trigger an unnecessary change detection in the application.
This
setTimeoutwas added in15e3978.
Both tests added in that commit still pass. Additionally, the comment
for why the
setTimeoutwas added doesn't really explain how thedescribed bug would occur. There has been a lot of work in the Router
since then to stabalize edge case scenarios so it's possible it existed
before but doesn't anymore.
Removing this
setTimeoutrevealed tests thatrelied on the navigation not completing. For example, the test suite did
not have a route which matched the redirect, but the test passed because
it ended before the redirect was flushed, so the
Routernever threw anerror. Similar situations exist for the other use of
setTimeoutin the Route(the one in the location change listener).
There were no other failures in TGP other than incorrectly written
tests.
BREAKING CHANGE:
When a guard returns a
UrlTree, the router would previously schedulethe redirect navigation within a
setTimeout. This timeout is now removed,which can result in test failures due to incorrectly written tests.
Tests which perform navigations should ensure that all timeouts are
flushed before making assertions. Tests should ensure they are capable
of handling all redirects from the original navigation.