Skip to content

bug: The navigation direction is preserved after calling any of the NavController's navigate methods while being on the same route #30549

@miroslavgrozdanovski

Description

@miroslavgrozdanovski

Prerequisites

Ionic Framework Version

v7.x

Current Behavior

If I'm already on a given page/route (e.g. "/home"), and I call navController.navigateRoot('/home'), the root direction is not "used" and it's preserved for any future navigation I use. So if after that navigation, I just click on any link using a standard routerLink, that navigation is done with a root direction instead of the default "forward".

Expected Behavior

When the navController.navigateRoot('/home') finishes, the root direction should be changed back to default, regardless if the navigation really happened or not (depending on whether you were already on that page). Not reverting the direction to the default value is very dangerous and causes very weird bugs, as your next navigation just uses the previously root direction without you wanting that.

Steps to Reproduce

  1. Go to any route (e.g. '/home').
  2. Call navController.navigateRoot('/home')
  3. Do any other standard navigation by clicking on a routerLink element or using a normal router.navigateByUrl('/another-page') method to a different route.
  4. Observe how the last navigation is done using root direction. This is most easily noticeable if you have an ion-back-button on the new page and it just doesn't show.

Code Reproduction URL

https://stackblitz.com/edit/lwufjbgw-pht97uge?file=src%2Fapp%2Fexample.component.html

Ionic Info

Ionic:
Ionic CLI : 7.2.0 (C:\Users\XXX\AppData\Roaming\nvm\v20.18.0\node_modules@ionic\cli)
Ionic Framework : @ionic/angular 8.6.3
@angular-devkit/build-angular : 18.2.11
@angular-devkit/schematics : 18.2.11
@angular/cli : 18.2.11
@ionic/angular-toolkit : 11.0.1

Capacitor:
Capacitor CLI : 6.1.2
@capacitor/android : 6.1.2
@capacitor/core : 6.1.2
@capacitor/ios : 6.1.2

Utility:
cordova-res : not installed globally
native-run : 2.0.1

System:
NodeJS : v20.18.0 (C:\Program Files\nodejs\node.exe)
npm : 10.8.2
OS : Windows 10

Additional Information

I know I can theoretically check whether I'm on the route already and avoid calling any of the navigate methods if I'm already there, or call navController.setDirection('forward') after calling navController.navigateRoot(X), but I think that that should be handled at the framework level, as the workarounds are weird and not intuitive.

I coded the example in the Stackblitz link, but for some reason, routerLink wasn't working for me at all in that environment. If someone has an idea why, the reproduction example should otherwise fully work.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions