diff --git a/packages/react-router/src/ReactRouter/StackManager.tsx b/packages/react-router/src/ReactRouter/StackManager.tsx index 8ab592787fa..b8fe0d3bc93 100644 --- a/packages/react-router/src/ReactRouter/StackManager.tsx +++ b/packages/react-router/src/ReactRouter/StackManager.tsx @@ -631,6 +631,12 @@ export class StackManager extends React.PureComponent { const shouldSkipAnimation = this.applySkipAnimationIfNeeded(enteringViewItem, leavingViewItem); + // When the transition has no leaving page to hide (the leaving view + // lookup missed), hide any other visible view in this outlet so a + // preserved or stale page does not stay on screen. See + // hideOtherVisibleViews. + this.hideOtherVisibleViews(enteringViewItem, leavingViewItem); + this.transitionPage(routeInfo, enteringViewItem, leavingViewItem, undefined, false, shouldSkipAnimation); if (shouldUnmountLeavingViewItem && leavingViewItem && enteringViewItem !== leavingViewItem) { @@ -868,6 +874,46 @@ export class StackManager extends React.PureComponent { } } + /** + * Hides every other visible view in this outlet when the transition has no + * leaving page to hide. + * + * This covers the cases where the leaving view lookup misses: re-entering + * an outlet whose preserved page is still visible after the swipe-back + * preservation in handleOutOfScopeOutlet (#31708), and a redirect that + * lands before the intermediate route's page registers (#31709 path B). + * In both cases the entering view is ready and the transition hides + * nothing, so any other visible view would stay on screen. + * + * Mirrors the hide-all-others fallback in handleWaitingForIonPage (the + * Navigate branch and the ionPageWaitTimeout path), except the leaving + * view is excluded here: transitionPage hides it once the entering view + * is visible, and hiding it early would break the transition animation. + */ + private hideOtherVisibleViews(enteringViewItem: ViewItem, leavingViewItem: ViewItem | undefined): void { + if (leavingViewItem?.ionPageElement) { + return; + } + + const enteringRoutePath = enteringViewItem.reactElement?.props?.path as string | undefined; + const allViewsInOutlet = this.context.getViewItemsForOutlet(this.id); + for (const viewItem of allViewsInOutlet) { + if (viewItem === enteringViewItem || viewItem === leavingViewItem) { + continue; + } + if (!viewItem.mount || !viewItem.ionPageElement || !isViewVisible(viewItem.ionPageElement)) { + continue; + } + // Don't hide container routes when entering a container route + // (e.g., /tabs/* and /settings/* coexist for tab switching). + const viewRoutePath = viewItem.reactElement?.props?.path as string | undefined; + if (viewRoutePath?.endsWith('/*') && enteringRoutePath?.endsWith('/*')) { + continue; + } + hideIonPageElement(viewItem.ionPageElement); + } + } + /** * Determines whether to skip the transition animation and, if so, immediately * hides the leaving view with inline `visibility:hidden`. @@ -1674,6 +1720,18 @@ export class StackManager extends React.PureComponent { showIonPageElement(leavingEl); } + /** + * Mirror of the repair above: a newer transitionPage call ran while this + * commit was in flight and showed a different page, but the core commit + * has just made this transition's entering page visible again. Hide it + * so the newer transition's DOM state wins. Without this, a redirect + * that lands mid-transition leaves the redirecting route's page visible + * on top (#31709 path A). + */ + if (myGeneration !== this.transitionGeneration && enteringEl !== this.transitionEnteringElement) { + hideIonPageElement(enteringEl); + } + if (!progressAnimation) { enteringEl.classList.remove('ion-page-invisible'); } diff --git a/packages/react-router/test/base/src/App.tsx b/packages/react-router/test/base/src/App.tsx index 983e79bb6cf..fddb4214832 100644 --- a/packages/react-router/test/base/src/App.tsx +++ b/packages/react-router/test/base/src/App.tsx @@ -63,6 +63,7 @@ import DisabledButton from './pages/disabled-button/DisabledButton'; import SplatSibling from './pages/splat-sibling/SplatSibling'; import StaleRegister from './pages/stale-register/StaleRegister'; import { EmptyPathSibling, IndexSibling } from './pages/index-sibling/IndexSibling'; +import RedirectStaleCommit from './pages/redirect-stale-commit/RedirectStaleCommit'; const App: React.FC = () => { return ( @@ -141,6 +142,7 @@ const App: React.FC = () => { /> } /> } /> + } /> diff --git a/packages/react-router/test/base/src/pages/redirect-stale-commit/RedirectStaleCommit.tsx b/packages/react-router/test/base/src/pages/redirect-stale-commit/RedirectStaleCommit.tsx new file mode 100644 index 00000000000..e469ca685aa --- /dev/null +++ b/packages/react-router/test/base/src/pages/redirect-stale-commit/RedirectStaleCommit.tsx @@ -0,0 +1,158 @@ +import { + IonButton, + IonContent, + IonHeader, + IonPage, + IonRouterOutlet, + IonTabBar, + IonTabButton, + IonTabs, + IonTitle, + IonToolbar, + useIonRouter, +} from '@ionic/react'; +import React from 'react'; +import { Navigate, Route, useSearchParams } from 'react-router-dom'; + +import TestDescription from '../../components/TestDescription'; + +/** + * Regression page for ionic-team/ionic-framework#31709: when the app pushes + * a route in an `IonTabs` outlet and that route immediately redirects, the + * tabs outlet can end up with two pages that are not hidden. + * + * - Path A: the redirect lands while the forward transition to the + * redirecting route is still running. The empty `ionPage` outlet of the + * redirecting route (Banana) is left visible on top. + * - Path B: the redirect lands before the redirecting route's page + * registers. The tab the user pushed from (Orange) is never hidden. + * + * Apple, Orange and Banana are the three `ionPage` outlets in the tabs + * outlet. The Banana outlet's only child, `redirect`, renders a + * `` after `delay` ms (from the + * `?delay=` query param), or immediately when its "Redirect now" button + * is pressed (used by the path A test to land the redirect while the + * forward transition is in flight). + */ +const ApplePage: React.FC = () => { + return ( + + + + Apple + + + + + Go to the Orange tab, then press a redirect button. After the redirect only the Apple outlet should be + visible. + + + + ); +}; + +const OrangePage: React.FC = () => { + const router = useIonRouter(); + return ( + + + + Orange + + + + router.push('/redirect-stale-commit/banana/redirect?delay=0', 'forward', 'push')} + > + Redirect after 0 ms + + router.push('/redirect-stale-commit/banana/redirect?delay=10', 'forward', 'push')} + > + Redirect after 10 ms + + router.push('/redirect-stale-commit/banana/redirect', 'forward', 'push')} + > + Go to Banana (redirect manually) + + + + ); +}; + +const RedirectToApple: React.FC = () => { + const [searchParams] = useSearchParams(); + const delayParam = searchParams.get('delay'); + const delay = delayParam === null ? null : Number(delayParam); + const [redirect, setRedirect] = React.useState(false); + const router = useIonRouter(); + + React.useEffect(() => { + if (delay === null) { + return; + } + const timeout = setTimeout(() => setRedirect(true), delay); + return () => clearTimeout(timeout); + }, [delay]); + + if (redirect) { + return ; + } + return ( + + + router.push('/redirect-stale-commit/apple', 'none', 'replace')}> + Redirect now + + + + ); +}; + +const RedirectStaleCommit: React.FC = () => { + return ( + + + + } /> + + } + /> + + } /> + + } + /> + + } /> + + } + /> + + + + Apple + + + Orange + + + + ); +}; + +export default RedirectStaleCommit; diff --git a/packages/react-router/test/base/tests/e2e/playwright/redirect-stale-commit.spec.ts b/packages/react-router/test/base/tests/e2e/playwright/redirect-stale-commit.spec.ts new file mode 100644 index 00000000000..41392aac95e --- /dev/null +++ b/packages/react-router/test/base/tests/e2e/playwright/redirect-stale-commit.spec.ts @@ -0,0 +1,88 @@ +import { test, expect } from '@playwright/test'; +import { ionPageVisible } from './utils/test-utils'; + +/** + * Regression tests for ionic-team/ionic-framework#31709: when the app + * pushes a route in an `IonTabs` outlet and that route immediately + * redirects, the tabs outlet can end up with two pages that are not + * hidden. + * + * - Path A: the redirect lands while the forward transition to the + * redirecting route is still running. The empty Banana outlet is left + * visible on top of Apple. The test triggers the redirect manually once + * both the Orange (leaving) and Banana (entering) outlets are visible, + * i.e. while the forward transition is in flight. (A fixed millisecond + * delay proved too timing-sensitive in this environment: 10 ms never + * reproduced it here. `ion-page-invisible` is not a usable signal + * either: a freshly mounted outlet is already visible when the commit + * runs, so the class is never added.) + * - Path B: the redirect lands before the redirecting route's page + * registers (0 ms timer). The Orange outlet the user pushed from is + * never hidden. + * + * After the redirect, only the Apple outlet should be visible in the tabs + * outlet. (In the unfixed build the 0 ms case mostly shows path B and + * sometimes path A, so both outlets are asserted hidden in both tests.) + */ +test.describe('redirect from a pushed tab route', () => { + test('redirect mid-transition: only the Apple outlet stays visible (path A)', async ({ page }) => { + await page.goto('/redirect-stale-commit/apple'); + await ionPageVisible(page, 'redirect-apple'); + + await page.locator('#tab-orange').click(); + await ionPageVisible(page, 'redirect-orange'); + + await page.locator('#redirect-manual').click(); + + // Redirect while the forward transition is in flight. The whole + // transition lasts only ~30 ms in this environment and a Playwright + // round-trip takes about as long, so the wait and the click both happen + // inside the page: poll on requestAnimationFrame and click as soon as + // the entering Banana outlet is shown while the leaving Orange outlet + // is still visible. + await page.evaluate(() => { + return new Promise((resolve) => { + const check = () => { + const banana = document.querySelector('#banana-outlet'); + const orange = document.querySelector('#orange-outlet'); + const button = document.querySelector('#redirect-now') as HTMLElement | null; + if ( + banana !== null && + orange !== null && + button !== null && + !banana.classList.contains('ion-page-hidden') && + !orange.classList.contains('ion-page-hidden') + ) { + button.click(); + resolve(); + } else { + requestAnimationFrame(check); + } + }; + check(); + }); + }); + + await expect(page).toHaveURL(/\/redirect-stale-commit\/apple$/); + await ionPageVisible(page, 'redirect-apple'); + await expect(page.locator('#banana-outlet')).toHaveClass(/ion-page-hidden/); + await expect(page.locator('#orange-outlet')).toHaveClass(/ion-page-hidden/); + await expect(page.locator('#apple-outlet')).not.toHaveClass(/ion-page-hidden/); + }); + + test('0 ms redirect: only the Apple outlet stays visible (path B)', async ({ page }) => { + await page.goto('/redirect-stale-commit/apple'); + await ionPageVisible(page, 'redirect-apple'); + + await page.locator('#tab-orange').click(); + await ionPageVisible(page, 'redirect-orange'); + + await page.locator('#redirect-0ms').click(); + + await expect(page).toHaveURL(/\/redirect-stale-commit\/apple$/); + await ionPageVisible(page, 'redirect-apple'); + await expect(page.locator('#orange-outlet')).toHaveClass(/ion-page-hidden/); + await expect(page.locator('#banana-outlet')).toHaveClass(/ion-page-hidden/); + await expect(page.locator('#apple-outlet')).not.toHaveClass(/ion-page-hidden/); + }); +});