Skip to content

Commit b40f6f3

Browse files
jasonadenkara
authored andcommitted
fix(router): ensure history.state is set in eager update mode (#30154)
Without this change, `history.state` isn't being set when updating the browser URL in `eager` update mode. PR Close #30154
1 parent 3327bd8 commit b40f6f3

2 files changed

Lines changed: 30 additions & 1 deletion

File tree

‎packages/router/src/router.ts‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -516,7 +516,8 @@ export class Router {
516516
tap(t => {
517517
if (this.urlUpdateStrategy === 'eager') {
518518
if (!t.extras.skipLocationChange) {
519-
this.setBrowserUrl(t.urlAfterRedirects, !!t.extras.replaceUrl, t.id);
519+
this.setBrowserUrl(
520+
t.urlAfterRedirects, !!t.extras.replaceUrl, t.id, t.extras.state);
520521
}
521522
this.browserUrlTree = t.urlAfterRedirects;
522523
}

‎packages/router/test/integration.spec.ts‎

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -631,6 +631,7 @@ describe('Integration', () => {
631631
advance(fixture);
632632
expect(fixture.nativeElement).toHaveText('team 33 [ , right: ]');
633633
})));
634+
634635
it('should eagerly update the URL with urlUpdateStrategy="eagar"',
635636
fakeAsync(inject([Router, Location], (router: Router, location: Location) => {
636637
const fixture = TestBed.createComponent(RootCmp);
@@ -722,6 +723,33 @@ describe('Integration', () => {
722723
expect(fixture.nativeElement).toHaveText('team 33 [ , right: ]');
723724
})));
724725

726+
it('should should set `state` with urlUpdateStrategy="eagar"',
727+
fakeAsync(inject([Router, Location], (router: Router, location: SpyLocation) => {
728+
729+
router.urlUpdateStrategy = 'eager';
730+
router.resetConfig([
731+
{path: '', component: SimpleCmp},
732+
{path: 'simple', component: SimpleCmp},
733+
]);
734+
735+
const fixture = createRoot(router, RootCmp);
736+
let navigation: Navigation = null !;
737+
router.events.subscribe(e => {
738+
if (e instanceof NavigationStart) {
739+
navigation = router.getCurrentNavigation() !;
740+
}
741+
});
742+
743+
router.navigateByUrl('/simple', {state: {foo: 'bar'}});
744+
tick();
745+
746+
const history = (location as any)._history;
747+
expect(history[history.length - 1].state.foo).toBe('bar');
748+
expect(history[history.length - 1].state)
749+
.toEqual({foo: 'bar', navigationId: history.length});
750+
expect(navigation.extras.state).toBeDefined();
751+
expect(navigation.extras.state).toEqual({foo: 'bar'});
752+
})));
725753
});
726754

727755
it('should navigate back and forward',

0 commit comments

Comments
 (0)