From f4e395d0149c429d23676138002a5268fc90f3bb Mon Sep 17 00:00:00 2001 From: Asish Kumar Date: Sat, 7 Nov 2026 12:04:08 +0530 Subject: [PATCH] fix(angular): ignore swipe back with popped stack Return early from startBackTransition when there is no view to go back to. The swipe gesture checks canGoBack when the pointer goes down, but only starts the transition once the pointer passes the gesture threshold. A navigation finishing in between left a single view in the stack, so reading animationBuilder from the missing entering view threw a TypeError. The throw also left the outlet's gesture marked as in progress, which disabled swipe back until the outlet was recreated. closes #31291 --- .../directives/navigation/stack-controller.ts | 10 ++++ .../swipe-back-popped-stack.spec.ts | 56 +++++++++++++++++++ 2 files changed, 66 insertions(+) create mode 100644 packages/angular/test/base/e2e/src/standalone/swipe-back-popped-stack.spec.ts diff --git a/packages/angular/src/common/directives/navigation/stack-controller.ts b/packages/angular/src/common/directives/navigation/stack-controller.ts index 94a7faf0b23..4e5067e1d67 100644 --- a/packages/angular/src/common/directives/navigation/stack-controller.ts +++ b/packages/angular/src/common/directives/navigation/stack-controller.ts @@ -182,6 +182,16 @@ export class StackController { if (leavingView) { const views = this.getStack(leavingView.stackId); const enteringView = views[views.length - 2]; + + /** + * The swipe gesture checks canGoBack when the pointer goes down, but + * only starts the transition once the pointer has moved far enough. + * A navigation that finishes in between can leave nothing to go back to. + */ + if (!enteringView) { + return Promise.resolve(); + } + const customAnimation = enteringView.animationBuilder; return this.wait(() => { diff --git a/packages/angular/test/base/e2e/src/standalone/swipe-back-popped-stack.spec.ts b/packages/angular/test/base/e2e/src/standalone/swipe-back-popped-stack.spec.ts new file mode 100644 index 00000000000..1b6b3b227eb --- /dev/null +++ b/packages/angular/test/base/e2e/src/standalone/swipe-back-popped-stack.spec.ts @@ -0,0 +1,56 @@ +import { expect, test } from '@playwright/test'; +import { ionSwipeToGoBack } from '../../utils/drag-utils'; +import { ionPageVisible, ionPageHidden, ionPageDoesNotExist } from '../../utils/test-utils'; + +test.describe('Swipe Back: Popped Stack', () => { + test.beforeEach(async ({ page }) => { + await page.goto('/standalone/swipe-animated-disabled?ionic:mode=ios'); + }); + + test('should ignore a swipe when a navigation pops the stack before the gesture starts', async ({ page }, testInfo) => { + testInfo.annotations.push({ + type: 'issue', + description: 'https://github.com/ionic-team/ionic-framework/issues/31291', + }); + + const pageErrors: Error[] = []; + page.on('pageerror', (error) => pageErrors.push(error)); + + await ionPageVisible(page, 'app-swipe-animated-disabled-main'); + + // Only an animated outlet starts the back transition when the gesture starts. + await page.locator('#toggle-animated').click(); + await expect(page.locator('#animated-value')).toHaveText('true'); + + await page.locator('#swipe-animated-disabled-details').click(); + await ionPageVisible(page, 'app-swipe-animated-disabled-details'); + await ionPageHidden(page, 'app-swipe-animated-disabled-main'); + + const y = page.viewportSize()!.height / 2; + + // The gesture checks that it can go back when the pointer goes down. + await page.mouse.move(5, y); + await page.mouse.down(); + + // Leave nothing to go back to before the pointer passes the gesture threshold. + await page.evaluate(() => window.history.back()); + await ionPageVisible(page, 'app-swipe-animated-disabled-main'); + await ionPageDoesNotExist(page, 'app-swipe-animated-disabled-details'); + + await page.mouse.move(35, y, { steps: 5 }); + await page.mouse.up(); + + await ionPageVisible(page, 'app-swipe-animated-disabled-main'); + expect(pageErrors).toEqual([]); + + // The ignored swipe should not leave the gesture stuck in progress. + await page.locator('#swipe-animated-disabled-details').click(); + await ionPageVisible(page, 'app-swipe-animated-disabled-details'); + await ionPageHidden(page, 'app-swipe-animated-disabled-main'); + + await ionSwipeToGoBack(page, true); + + await ionPageVisible(page, 'app-swipe-animated-disabled-main'); + await ionPageDoesNotExist(page, 'app-swipe-animated-disabled-details'); + }); +});