Skip to content

bug: ion-nav canGoBack can return incorrect value during transitions #21668

@cjorasch

Description

@cjorasch

Bug Report

Ionic version:
[x] 5.2.3

Current behavior:
When calling nav.canGoBack() it can return an incorrect value if navigation is in progress.

For example, if the following code is used:

const canGoBack = await nav.canGoBack();
if (canGoBack) return nav.pop();

If this code is called multiple times quickly then you get a result where canGoBack is true but there is an error during pop(). The following appears in the console:

You can't remove all the pages in the navigation stack. nav.pop() is probably called too many times. 
Nav {ionNavWillLoad: {…}, ionNavWillChange: {…}, ionNavDidChange: {…}, transInstr: Array(1), animationEnabled: true, …} 

Uncaught (in promise) Error: navigation stack needs at least one root page
    at Nav.postViewInit (p-56c309e2.entry.js:644)
    at Nav.runTransition (p-56c309e2.entry.js:506)
    at Nav.nextTrns (p-56c309e2.entry.js:489)
    at Nav.runTransition (p-56c309e2.entry.js:538)

There is a workaround in ion-back-button that uses the skipIfBusy option to ignore overlapping calls but this does not work in situations where application logic is using the canGoBack value. For example, if you want to have a button that goes back if the user is in a stack or displays the home page if not in a navigation stack.

canGoBack should detect the impact of in-progress changes so that it can correctly indicate whether pop is available.

Other ion-nav methods may have similar issues where they do not reflect in-progress changes to the stack.

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