Skip to content

Commit e34299a

Browse files
atscottzarend
authored andcommitted
fix(router): handle new navigations from a NavigationEnd event (#41262) (#41511)
This commit removes the line to set `currentNavigation` to `null` in the navigation transitions subscription of the router. This logic is already handled in the `finalize` stage of the transition pipe and has been found to cause issues if a new navigation is triggered from a subscription to the `NavigationEnd` event. fixes #37460 PR Close #41262 PR Close #41511
1 parent 74d1769 commit e34299a

File tree

2 files changed

+35
-4
lines changed

2 files changed

+35
-4
lines changed

packages/router/src/router.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1245,7 +1245,6 @@ export class Router {
12451245
.next(new NavigationEnd(
12461246
t.id, this.serializeUrl(t.extractedUrl), this.serializeUrl(this.currentUrlTree)));
12471247
this.lastSuccessfulNavigation = this.currentNavigation;
1248-
this.currentNavigation = null;
12491248
t.resolve(true);
12501249
},
12511250
e => {

packages/router/test/bootstrap.spec.ts

Lines changed: 35 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,10 @@ describe('bootstrap', () => {
1919
let log: any[] = [];
2020
let testProviders: any[] = null!;
2121

22+
@Component({template: 'simple'})
23+
class SimpleCmp {
24+
}
25+
2226
@Component({selector: 'test-app', template: 'root <router-outlet></router-outlet>'})
2327
class RootCmp {
2428
constructor() {
@@ -369,7 +373,35 @@ describe('bootstrap', () => {
369373
done();
370374
});
371375

372-
function waitForNavigationToComplete(router: Router): Promise<any> {
373-
return router.events.pipe(filter((e: any) => e instanceof NavigationEnd), first()).toPromise();
374-
}
376+
it('can schedule a navigation from the NavigationEnd event #37460', async (done) => {
377+
@NgModule({
378+
imports: [
379+
BrowserModule,
380+
RouterModule.forRoot(
381+
[
382+
{path: 'a', component: SimpleCmp},
383+
{path: 'b', component: SimpleCmp},
384+
],
385+
)
386+
],
387+
declarations: [RootCmp, SimpleCmp],
388+
bootstrap: [RootCmp],
389+
providers: [...testProviders],
390+
})
391+
class TestModule {
392+
}
393+
394+
const res = await platformBrowserDynamic([]).bootstrapModule(TestModule);
395+
const router = res.injector.get(Router);
396+
router.events.subscribe(() => {
397+
expect(router.getCurrentNavigation()?.id).toBeDefined();
398+
});
399+
router.events.subscribe(async (e) => {
400+
if (e instanceof NavigationEnd && e.url === '/b') {
401+
await router.navigate(['a']);
402+
done();
403+
}
404+
});
405+
await router.navigateByUrl('/b');
406+
});
375407
});

0 commit comments

Comments
 (0)