Repository navigation
Description
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
- A list page whose items navigate forward with
RouterExtensions.navigate()ornsRouterLink, inside apage-router-outlet. - Open an item, go back, and tap another item as soon as the list is visible.
- 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