fix(router): adjust setting navigationTransition when a new navigation cancels an existing one (#29636)
Prior to this change, if a navigation was ongoing and a new one came in, the router could get into a state where `router.currentNavigation` was `null` even though a navigation was executing. This change moves where we set the `currentNavigation` value so it's inside a `switchMap`. This solves the problem because the `finally` on the `switchMap` had been setting `currentNavigation` to `null` but the new `currentNavigation` value would have already been set. Essentially this was a timing problem and is resolved with this change. Fixes #29389 #29590 PR Close #29636
This commit is contained in:
parent
d7e5535d00
commit
f9497bf28b
|
@ -452,6 +452,11 @@ export class Router {
|
||||||
...t, extractedUrl: this.urlHandlingStrategy.extract(t.rawUrl)
|
...t, extractedUrl: this.urlHandlingStrategy.extract(t.rawUrl)
|
||||||
} as NavigationTransition)),
|
} as NavigationTransition)),
|
||||||
|
|
||||||
|
// Using switchMap so we cancel executing navigations when a new one comes in
|
||||||
|
switchMap(t => {
|
||||||
|
let completed = false;
|
||||||
|
let errored = false;
|
||||||
|
return of (t).pipe(
|
||||||
// Store the Navigation object
|
// Store the Navigation object
|
||||||
tap(t => {
|
tap(t => {
|
||||||
this.currentNavigation = {
|
this.currentNavigation = {
|
||||||
|
@ -465,12 +470,6 @@ export class Router {
|
||||||
null
|
null
|
||||||
};
|
};
|
||||||
}),
|
}),
|
||||||
|
|
||||||
// Using switchMap so we cancel executing navigations when a new one comes in
|
|
||||||
switchMap(t => {
|
|
||||||
let completed = false;
|
|
||||||
let errored = false;
|
|
||||||
return of (t).pipe(
|
|
||||||
switchMap(t => {
|
switchMap(t => {
|
||||||
const urlTransition =
|
const urlTransition =
|
||||||
!this.navigated || t.extractedUrl.toString() !== this.browserUrlTree.toString();
|
!this.navigated || t.extractedUrl.toString() !== this.browserUrlTree.toString();
|
||||||
|
|
|
@ -1067,6 +1067,27 @@ describe('Integration', () => {
|
||||||
]);
|
]);
|
||||||
})));
|
})));
|
||||||
|
|
||||||
|
it('should properly set currentNavigation when cancelling in-flight navigations',
|
||||||
|
fakeAsync(inject([Router], (router: Router) => {
|
||||||
|
const fixture = createRoot(router, RootCmp);
|
||||||
|
|
||||||
|
router.resetConfig([{path: 'user/:name', component: UserCmp}]);
|
||||||
|
|
||||||
|
router.navigateByUrl('/user/init');
|
||||||
|
advance(fixture);
|
||||||
|
|
||||||
|
router.navigateByUrl('/user/victor');
|
||||||
|
expect((router as any).currentNavigation).not.toBe(null);
|
||||||
|
router.navigateByUrl('/user/fedor');
|
||||||
|
// Due to https://github.com/angular/angular/issues/29389, this would be `false`
|
||||||
|
// when running a second navigation.
|
||||||
|
expect((router as any).currentNavigation).not.toBe(null);
|
||||||
|
advance(fixture);
|
||||||
|
|
||||||
|
expect((router as any).currentNavigation).toBe(null);
|
||||||
|
expect(fixture.nativeElement).toHaveText('user fedor');
|
||||||
|
})));
|
||||||
|
|
||||||
it('should handle failed navigations gracefully', fakeAsync(inject([Router], (router: Router) => {
|
it('should handle failed navigations gracefully', fakeAsync(inject([Router], (router: Router) => {
|
||||||
const fixture = createRoot(router, RootCmp);
|
const fixture = createRoot(router, RootCmp);
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue