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
refactor(router): Remove unnecessary setTimeout in UrlTree redirects #45735
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
|
TGP |
|
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. |



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.