Skip to content

Commit 1f287b9

Browse files
atscottmattrbeck
authored andcommitted
refactor(router): Move target RouterState creation before 'blocking' stage
This change moves `RouterState` creation to _before_ the `afterPreactivation` step, which is the step that pauses until bootstrap listeners are complete. It is used for 'enabled blocking' initial navigation and destructive hydration. After this stage, activation is expected to be (more or less) synchronous. More importantly than above (since enabled blocking and destructive hydration are essentially deprecated), this also oves the state creation before the view transition creation. These are done to accomodate features in the future that would depend on the RouterState (e.g. ones which need to know which `ActivatedRoute` instances are new and which are reused). These features may include additional async blocks/waits, which should not happen after view transition creation (which freezes the UI until resolved).
1 parent e661f4d commit 1f287b9

1 file changed

Lines changed: 17 additions & 12 deletions

File tree

packages/router/src/navigation_transition.ts

Lines changed: 17 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -443,6 +443,7 @@ export class NavigationTransitions {
443443

444444
// Using switchMap so we cancel executing navigations when a new one comes in
445445
switchMap((overallTransitionState) => {
446+
let abortable = true;
446447
let completedOrAborted = false;
447448
const abortController = new AbortController();
448449
const shouldContinueNavigation = () => {
@@ -738,6 +739,20 @@ export class NavigationTransitions {
738739
return loaders.length === 0 ? of(t) : from(Promise.all(loaders).then(() => t));
739740
}),
740741

742+
switchMap((t: NavigationTransition) => {
743+
const targetRouterState = createRouterState(
744+
router.routeReuseStrategy,
745+
t.targetSnapshot!,
746+
t.currentRouterState,
747+
);
748+
this.currentTransition = overallTransitionState = t = {...t, targetRouterState};
749+
this.currentNavigation.update((nav) => {
750+
nav!.targetRouterState = targetRouterState;
751+
return nav;
752+
});
753+
return of(t);
754+
}),
755+
741756
switchTap(() => this.afterPreactivation()),
742757

743758
// TODO(atscott): Move this into the last block below.
@@ -762,17 +777,7 @@ export class NavigationTransitions {
762777
take(1),
763778

764779
switchMap((t: NavigationTransition) => {
765-
const targetRouterState = createRouterState(
766-
router.routeReuseStrategy,
767-
t.targetSnapshot!,
768-
t.currentRouterState,
769-
);
770-
this.currentTransition = overallTransitionState = t = {...t, targetRouterState};
771-
this.currentNavigation.update((nav) => {
772-
nav!.targetRouterState = targetRouterState;
773-
return nav;
774-
});
775-
780+
abortable = false;
776781
this.events.next(new BeforeActivateRoutes());
777782
const deferred = overallTransitionState.beforeActivateHandler.deferredHandle;
778783
return deferred ? from(deferred.then(() => t)) : of(t);
@@ -811,7 +816,7 @@ export class NavigationTransitions {
811816
takeUntil(
812817
abortSignalToObservable(abortController.signal).pipe(
813818
// Ignore aborts if we are already completed, canceled, or are in the activation stage (we have targetRouterState)
814-
filter(() => !completedOrAborted && !overallTransitionState.targetRouterState),
819+
filter(() => !completedOrAborted && abortable),
815820
tap(() => {
816821
this.cancelNavigationTransition(
817822
overallTransitionState,

0 commit comments

Comments
 (0)