Skip to content

Navigating right after a native back is undone by the Router's deferred popstate #185

Backlog

Description

@anjak03

Environment

  • CLI: 9.1.1
  • Cross-platform modules: @nativescript/core 9.1.4
  • Android Runtime: 9.1.1
  • iOS Runtime: 9.1.1
  • NativeScript-Angular: 18.0.1, the same code is on main
  • Angular: 18.0.3, the deferral below is unchanged in 22

Describe the bug
When a page is popped natively (back button, RouterExtensions.back(), swipe back), PageRouterOutlet syncs the Router in the page's navigatedFrom handler. _beginBackPageNavigation() marks the outlet as going back and NSLocationStrategy.back() fires a popstate. Angular's Router handles a popstate in a setTimeout (HistoryStateManager.registerNonRouterCurrentEntryChangeListener), so the previous page is already visible and accepts taps while the Router is still on the popped route.

A navigation that runs in that gap, for example a tap on a list item right after going back, goes first and clears the outlet's back mark. Then the deferred popstate runs with the URL captured at the back. The Router navigates away from the page that was just opened, and since the outlet is no longer marked as going back, the list is created again as a new forward page. The user ends up on a second copy of the list, and going back from it shows the page they tapped.

Trace from a production app on the iOS simulator (ms since launch, routes renamed):

23717 frame goBack
24680 detail page navigatedFrom back=true      <- native back done, popstate fired
24689 list page navigatedTo back=true          <- list visible
24697 tap, router start #7 imperative /list/other
24722 router end #7
24810 router start #8 popstate /list           <- the deferred popstate of the back
24822 router end #8
25196 other page navigatedTo                   <- the tapped page
25806 new list page navigatedTo                <- a second copy of the list

In runs without the problem the popstate navigation started 3 ms after navigatedTo. With the tap in between it started 120 ms later.

To Reproduce

  1. A list page whose items navigate forward with RouterExtensions.navigate() or nsRouterLink, inside a page-router-outlet.
  2. Open an item, go back, and tap another item as soon as the list is visible.
  3. The tapped page opens and a new copy of the list is pushed on top of it. Timing dependent, 2 of 3 automated runs on the iOS simulator.

Expected behavior
The tapped page opens and stays. The popstate of the back is applied before the new navigation.

Additional context
The setTimeout is still there in Angular 22 and RouterExtensions.navigate() / navigateByUrl() on main still call the Router directly, so main has the same gap. NSLocationStrategy._closeModalNavigation() already waits a setTimeout for the same reason.

Suggested fix: after callPopState() the location strategy keeps a promise that resolves in a setTimeout, so after the Router's, and RouterExtensions.navigate() / navigateByUrl() wait for it while it is pending. nsRouterLink navigates through RouterExtensions, so it is covered too.

Activity

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions