Skip to content

Commit 7b367d9

Browse files
committed
refactor(router): Remove unnecessary setTimeout in UrlTree redirects (#45735)
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 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. PR Close #45735
1 parent f1cc4a6 commit 7b367d9

1 file changed

Lines changed: 15 additions & 21 deletions

File tree

packages/router/src/router.ts

Lines changed: 15 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1000,27 +1000,21 @@ export class Router {
10001000
if (!redirecting) {
10011001
t.resolve(false);
10021002
} else {
1003-
// setTimeout is required so this navigation finishes with
1004-
// the return EMPTY below. If it isn't allowed to finish
1005-
// processing, there can be multiple navigations to the same
1006-
// URL.
1007-
setTimeout(() => {
1008-
const mergedTree =
1009-
this.urlHandlingStrategy.merge(e.url, this.rawUrlTree);
1010-
const extras = {
1011-
skipLocationChange: t.extras.skipLocationChange,
1012-
// The URL is already updated at this point if we have 'eager' URL
1013-
// updates or if the navigation was triggered by the browser (back
1014-
// button, URL bar, etc). We want to replace that item in history if
1015-
// the navigation is rejected.
1016-
replaceUrl: this.urlUpdateStrategy === 'eager' ||
1017-
isBrowserTriggeredNavigation(t.source)
1018-
};
1019-
1020-
this.scheduleNavigation(
1021-
mergedTree, 'imperative', null, extras,
1022-
{resolve: t.resolve, reject: t.reject, promise: t.promise});
1023-
}, 0);
1003+
const mergedTree =
1004+
this.urlHandlingStrategy.merge(e.url, this.rawUrlTree);
1005+
const extras = {
1006+
skipLocationChange: t.extras.skipLocationChange,
1007+
// The URL is already updated at this point if we have 'eager' URL
1008+
// updates or if the navigation was triggered by the browser (back
1009+
// button, URL bar, etc). We want to replace that item in history if
1010+
// the navigation is rejected.
1011+
replaceUrl: this.urlUpdateStrategy === 'eager' ||
1012+
isBrowserTriggeredNavigation(t.source)
1013+
};
1014+
1015+
this.scheduleNavigation(
1016+
mergedTree, 'imperative', null, extras,
1017+
{resolve: t.resolve, reject: t.reject, promise: t.promise});
10241018
}
10251019

10261020
/* All other errors should reset to the router's internal URL reference to

0 commit comments

Comments
 (0)