Skip to content

fix(angular): keep the navigateRoot direction when a guard redirects - #31843

Open
mokshupadhyay wants to merge 1 commit into
ionic-team:mainfrom
mokshupadhyay:fix/nav-controller-guard-redirect
Open

mokshupadhyay wants to merge 1 commit into
ionic-team:mainfrom
mokshupadhyay:fix/nav-controller-guard-redirect

Conversation

@mokshupadhyay

Copy link
Copy Markdown

Issue number: resolves #31071


What is the current behavior?

Since #30955, NavController resets the requested direction to auto on every NavigationCancel. When a guard returns a UrlTree, Angular cancels the original navigation with NavigationCancellationCode.Redirect before starting the redirect, so navigateRoot('/guarded') loses its root direction and the redirected page is pushed onto the existing stack. The old pages stay, and the back button is shown. The same happens when a pending navigation is superseded by a navigateRoot call.

What is the new behavior?

  • A NavigationCancel with the code Redirect or SupersededByNewNavigation keeps the direction, animation and animation builder, because the navigation continues (the same way Angular's router treats those two codes). This is the change suggested on the issue.
  • Every other cancel code and NavigationError still reset them, so the case from fix(nav-controller): reset direction state when navigation is canceled #30955 (a guard rejecting a back navigation) is unchanged. The history-entry handling in that block is unchanged too.
  • New e2e test in back-history.spec.ts: from page C, navigateRoot to the existing /standalone/back-history/redirected route, whose guard redirects to D; D is shown and A, B and C are no longer in the DOM.

Does this introduce a breaking change?

  • Yes
  • No

Other information

Tested with the ng21 test app (headless Chromium): the new test fails on main (app-back-history-a still present) and passes with the change. With the change, the standalone back-history, nav, router-outlet, back-button, router-link and tabs specs and the lazy routing, navigation, router-link, nested-outlet, standalone-routing and tabs specs pass (83 tests). npm run lint and npm run test in packages/angular pass. I didn't run the other Angular versions' test apps locally.

An AI coding assistant (Claude Code) helped me write this; I reviewed the change and the test results.

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 ionic-team#31071
@mokshupadhyay
mokshupadhyay requested a review from a team as a code owner November 28, 2026 10:02
@vercel

vercel Bot commented Nov 28, 2026

Copy link
Copy Markdown

@mokshupadhyay is attempting to deploy a commit to the Ionic Team on Vercel.

A member of the Team first needs to authorize it.

@github-actions github-actions Bot added the package: angular @ionic/angular package label Nov 28, 2026

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

package: angular @ionic/angular package

Projects

None yet

Development

Successfully merging this pull request may close these issues.

bug: NavController direction gets lost when an Angular route guard returns a UrlTree

1 participant