Skip to content

Commit b0396e7

Browse files
atscottAndrewKushnir
authored andcommitted
fix(router): Ensure canceledNavigationResolution: 'computed' works on first page (#51441)
Setting the page ID is currently broken for the first page because the helper method's second parameter is optional, which allowed the initally `undefined` page ID to be used again when the router performs its initial navigation. fixes #50983 PR Close #51441
1 parent 7f0563e commit b0396e7

File tree

2 files changed

+12
-8
lines changed

2 files changed

+12
-8
lines changed

packages/router/src/router.ts

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -146,17 +146,17 @@ export class Router {
146146
* the router can then use this to compute how to restore the state back to the previously active
147147
* page.
148148
*/
149-
private currentPageId: number = 0;
149+
private currentPageId = 0;
150150
/**
151151
* The ɵrouterPageId of whatever page is currently active in the browser history. This is
152152
* important for computing the target page id for new navigations because we need to ensure each
153153
* page id in the browser history is 1 more than the previous entry.
154154
*/
155-
private get browserPageId(): number|undefined {
155+
private get browserPageId(): number {
156156
if (this.canceledNavigationResolution !== 'computed') {
157-
return undefined;
157+
return this.currentPageId;
158158
}
159-
return (this.location.getState() as RestoredState | null)?.ɵrouterPageId;
159+
return (this.location.getState() as RestoredState | null)?.ɵrouterPageId ?? this.currentPageId;
160160
}
161161
private console = inject(Console);
162162
private isNgZoneEnabled: boolean = false;
@@ -332,7 +332,7 @@ export class Router {
332332
this.navigationTransitions.setupNavigations(this).subscribe(
333333
t => {
334334
this.lastSuccessfulId = t.id;
335-
this.currentPageId = this.browserPageId ?? 0;
335+
this.currentPageId = this.browserPageId;
336336
},
337337
e => {
338338
this.console.warn(`Unhandled Navigation Error: ${e}`);
@@ -771,7 +771,7 @@ export class Router {
771771
} else {
772772
const state = {
773773
...transition.extras.state,
774-
...this.generateNgRouterState(transition.id, (this.browserPageId ?? 0) + 1)
774+
...this.generateNgRouterState(transition.id, this.browserPageId + 1)
775775
};
776776
this.location.go(path, '', state);
777777
}
@@ -784,7 +784,7 @@ export class Router {
784784
*/
785785
restoreHistory(transition: NavigationTransition, restoringFromCaughtError = false) {
786786
if (this.canceledNavigationResolution === 'computed') {
787-
const currentBrowserPageId = this.browserPageId ?? this.currentPageId;
787+
const currentBrowserPageId = this.browserPageId;
788788
const targetPagePosition = this.currentPageId - currentBrowserPageId;
789789
if (targetPagePosition !== 0) {
790790
this.location.historyGo(targetPagePosition);
@@ -832,7 +832,7 @@ export class Router {
832832
this.generateNgRouterState(this.lastSuccessfulId, this.currentPageId));
833833
}
834834

835-
private generateNgRouterState(navigationId: number, routerPageId?: number) {
835+
private generateNgRouterState(navigationId: number, routerPageId: number) {
836836
if (this.canceledNavigationResolution === 'computed') {
837837
return {navigationId, ɵrouterPageId: routerPageId};
838838
}

packages/router/test/computed_state_restoration.spec.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,10 @@ describe('`restoredState#ɵrouterPageId`', () => {
109109
},
110110
{path: 'loaded', loadChildren: () => of(ModuleWithSimpleCmpAsRoute), canLoad: ['alwaysFalse']}
111111
]);
112+
router.initialNavigation();
113+
advance(fixture);
114+
expect(location.getState()).toEqual(jasmine.objectContaining({ɵrouterPageId: 0}));
115+
112116
router.navigateByUrl('/first');
113117
advance(fixture);
114118
expect(location.getState()).toEqual(jasmine.objectContaining({ɵrouterPageId: 1}));

0 commit comments

Comments
 (0)