Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 15 additions & 5 deletions packages/angular/src/common/providers/nav-controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import {
NavigationStart,
NavigationEnd,
NavigationCancel,
NavigationCancellationCode,
NavigationError,
NavigationSkipped,
} from '@angular/router';
Expand Down Expand Up @@ -101,12 +102,21 @@ export class NavController {
this.commitHistoryEntry(ev.id);
}

// Reset explicit direction when navigation is canceled (e.g., guard rejection)
// to prevent stale direction from leaking into the next navigation
if (ev instanceof NavigationCancel || ev instanceof NavigationError) {
this.direction = DEFAULT_DIRECTION;
this.animated = DEFAULT_ANIMATED;
this.animationBuilder = undefined;
/**
* Reset the explicit direction when the navigation is canceled (e.g., a guard
* rejected it) so it doesn't leak into the next navigation. A redirect or a
* superseding navigation continues the same intent, so it keeps the direction.
*/
const continuesNavigation =
ev instanceof NavigationCancel &&
(ev.code === NavigationCancellationCode.Redirect ||
ev.code === NavigationCancellationCode.SupersededByNewNavigation);
if (!continuesNavigation) {
this.direction = DEFAULT_DIRECTION;
this.animated = DEFAULT_ANIMATED;
this.animationBuilder = undefined;
}

/**
* Drop the tracked history if the browser might not be on the tracked
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -196,6 +196,22 @@ test.describe('Back navigation browser history', () => {
await ionPageVisible(page, 'app-back-history-d');
});

test('should set the page a guard redirects to as root with navigateRoot', async ({ page }, testInfo) => {
testInfo.annotations.push({
type: 'issue',
description: 'https://github.com/ionic-team/ionic-framework/issues/31071',
});

await page.locator('#root-to-redirected').click();
await ionPageVisible(page, 'app-back-history-d');
await expect(page).toHaveURL(/\/standalone\/back-history\/d$/);

// The previous pages are removed from the stack.
await expect(page.locator('app-back-history-a')).toHaveCount(0);
await expect(page.locator('app-back-history-b')).toHaveCount(0);
await expect(page.locator('app-back-history-c')).toHaveCount(0);
});

test('ion-back-button should go back when the URL was changed outside the router', async ({ page }, testInfo) => {
annotateIssue(testInfo);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ import {
<ion-button id="go-to-wrapped" routerLink="/standalone/back-history/wrapped">Go to wrapped page</ion-button>
<ion-button id="go-to-resolved" routerLink="/standalone/back-history/resolved">Go to resolved page</ion-button>
<ion-button id="go-to-redirected" routerLink="/standalone/back-history/redirected">Go to a page that redirects to D</ion-button>
<ion-button id="root-to-redirected" (click)="rootToRedirected()">Set a page that redirects to D as root</ion-button>
<ion-button id="shallow-url-update" (click)="shallowUrlUpdate()">Update the URL outside the router</ion-button>
</ion-content>
`,
Expand All @@ -45,6 +46,10 @@ export class BackHistoryCComponent {
this.navCtrl.pop();
}

rootToRedirected() {
this.navCtrl.navigateRoot('/standalone/back-history/redirected');
}

shallowUrlUpdate() {
this.location.go('/standalone/back-history/c?shallow=1');
}
Expand Down