diff --git a/apps/nativescript-demo-ng/src/tests/ns-location-strategy.spec.ts b/apps/nativescript-demo-ng/src/tests/ns-location-strategy.spec.ts index 3746c4f..415dd35 100644 --- a/apps/nativescript-demo-ng/src/tests/ns-location-strategy.spec.ts +++ b/apps/nativescript-demo-ng/src/tests/ns-location-strategy.spec.ts @@ -1,6 +1,13 @@ // make sure you import mocha-config before @angular/core import { DefaultUrlSerializer, UrlTree } from '@angular/router'; -import { FrameService, LocationState, NavigationOptions, NSLocationStrategy, Outlet } from '@nativescript/angular'; +import { + FrameService, + LocationState, + NavigationOptions, + NSLocationStrategy, + Outlet, + RouterExtensions, +} from '@nativescript/angular'; import { BackstackEntry, Frame, NavigationEntry, Page, View } from '@nativescript/core'; export class FakeFrameService extends FrameService { @@ -508,4 +515,27 @@ describe('NSLocationStrategy', () => { assertStatesEqual(outlet.states, expectedStatesTest1); assertStatesEqual(outlet2.states, expectedStatesTest2); }); + + it('RouterExtensions navigates after the Router handled the popstate of a page back', async () => { + const { strategy, frameService } = initStrategy('/'); + const currentFrame = frameService.getFrame(); + const handled: Array = []; + // The Router handles a popstate in a setTimeout + strategy.onPopState(() => setTimeout(() => handled.push('popstate'))); + const router: any = { + navigate: () => { + handled.push('navigate'); + return Promise.resolve(true); + }, + }; + const routerExtensions = new RouterExtensions(router, strategy, frameService); + simulatePageNavigation(strategy, '/page', currentFrame, 'primary'); + + // Act + simulatePageBack(strategy, currentFrame); + await routerExtensions.navigate(['/other']); + + // Assert + expect(handled).toEqual(['popstate', 'navigate']); + }); }); diff --git a/packages/angular/src/lib/legacy/router/ns-location-strategy.ts b/packages/angular/src/lib/legacy/router/ns-location-strategy.ts index 7f3a7d5..d13c61a 100644 --- a/packages/angular/src/lib/legacy/router/ns-location-strategy.ts +++ b/packages/angular/src/lib/legacy/router/ns-location-strategy.ts @@ -15,6 +15,7 @@ export class NSLocationStrategy extends LocationStrategy implements OnDestroy { private currentOutlet: Outlet; private popStateCallbacks = new Array<(_: any) => any>(); + private popStateHandled: Promise | null = null; private currentUrlTree: UrlTree; public _modalNavigationDepth = 0; @@ -253,6 +254,22 @@ export class NSLocationStrategy extends LocationStrategy implements OnDestroy { for (const fn of this.popStateCallbacks) { fn(change); } + + // The Router handles a popstate in a setTimeout, this timeout runs after it + const handled = new Promise((resolve) => setTimeout(resolve, 0)).then(() => { + if (this.popStateHandled === handled) { + this.popStateHandled = null; + } + }); + this.popStateHandled = handled; + } + + /** + * Runs the navigation once the Router has handled the last popstate. + * Otherwise the Router would apply that popstate after the navigation and undo it. + */ + public _navigateAfterPopState(navigate: () => Promise): Promise { + return this.popStateHandled ? this.popStateHandled.then(navigate) : navigate(); } public toString() { diff --git a/packages/angular/src/lib/legacy/router/router-extensions.ts b/packages/angular/src/lib/legacy/router/router-extensions.ts index 90c2b1e..e003bef 100644 --- a/packages/angular/src/lib/legacy/router/router-extensions.ts +++ b/packages/angular/src/lib/legacy/router/router-extensions.ts @@ -21,11 +21,11 @@ export class RouterExtensions { constructor(public router: Router, public locationStrategy: NSLocationStrategy, public frameService: FrameService) {} public navigate(commands: any[], extras?: ExtendedNavigationExtras): Promise { - return this.router.navigate(commands, extras); + return this.locationStrategy._navigateAfterPopState(() => this.router.navigate(commands, extras)); } public navigateByUrl(url: string | UrlTree, options?: ExtendedNavigationBehaviorOptions): Promise { - return this.router.navigateByUrl(url, options); + return this.locationStrategy._navigateAfterPopState(() => this.router.navigateByUrl(url, options)); } public back(backNavigationOptions?: BackNavigationOptions) {