From 4c9d29ffbaaf4822b729ea9bc18b14fb4c108194 Mon Sep 17 00:00:00 2001 From: Moksh Upadhyay Date: Sat, 28 Nov 2026 01:29:28 +0530 Subject: [PATCH] fix(angular): keep the navigateRoot direction when a guard redirects A guard that returns a UrlTree cancels the navigation with a Redirect code before starting the redirect. Resetting the direction on that cancel pushed the redirected page onto the stack instead of setting it as root. Keep the direction, animation and animation builder when the navigation is redirected or superseded, and reset them for every other cancel or error. resolves #31071 --- .../src/common/providers/nav-controller.ts | 20 ++++++++++++++----- .../e2e/src/standalone/back-history.spec.ts | 16 +++++++++++++++ .../back-history/back-history-c.component.ts | 5 +++++ 3 files changed, 36 insertions(+), 5 deletions(-) diff --git a/packages/angular/src/common/providers/nav-controller.ts b/packages/angular/src/common/providers/nav-controller.ts index 216f2345384..73504cd2098 100644 --- a/packages/angular/src/common/providers/nav-controller.ts +++ b/packages/angular/src/common/providers/nav-controller.ts @@ -8,6 +8,7 @@ import { NavigationStart, NavigationEnd, NavigationCancel, + NavigationCancellationCode, NavigationError, NavigationSkipped, } from '@angular/router'; @@ -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 diff --git a/packages/angular/test/base/e2e/src/standalone/back-history.spec.ts b/packages/angular/test/base/e2e/src/standalone/back-history.spec.ts index 7d78cbc4f91..b80840c3bae 100644 --- a/packages/angular/test/base/e2e/src/standalone/back-history.spec.ts +++ b/packages/angular/test/base/e2e/src/standalone/back-history.spec.ts @@ -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); diff --git a/packages/angular/test/base/src/app/standalone/back-history/back-history-c.component.ts b/packages/angular/test/base/src/app/standalone/back-history/back-history-c.component.ts index 8c1359ab24f..5f3620bb06e 100644 --- a/packages/angular/test/base/src/app/standalone/back-history/back-history-c.component.ts +++ b/packages/angular/test/base/src/app/standalone/back-history/back-history-c.component.ts @@ -31,6 +31,7 @@ import { Go to wrapped page Go to resolved page Go to a page that redirects to D + Set a page that redirects to D as root Update the URL outside the router `, @@ -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'); }