Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
58 changes: 58 additions & 0 deletions packages/react-router/src/ReactRouter/StackManager.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -631,6 +631,12 @@ export class StackManager extends React.PureComponent<StackManagerProps> {

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) {
Expand Down Expand Up @@ -868,6 +874,46 @@ export class StackManager extends React.PureComponent<StackManagerProps> {
}
}

/**
* 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`.
Expand Down Expand Up @@ -1674,6 +1720,18 @@ export class StackManager extends React.PureComponent<StackManagerProps> {
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');
}
Expand Down
2 changes: 2 additions & 0 deletions packages/react-router/test/base/src/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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 (
Expand Down Expand Up @@ -141,6 +142,7 @@ const App: React.FC = () => {
/>
<Route path="/index-sibling/*" element={<IndexSibling />} />
<Route path="/empty-path-sibling/*" element={<EmptyPathSibling />} />
<Route path="/redirect-stale-commit/*" element={<RedirectStaleCommit />} />
</IonRouterOutlet>
</IonReactRouter>
</IonApp>
Expand Down
Original file line number Diff line number Diff line change
@@ -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
* `<Navigate replace to=".../apple" />` 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 (
<IonPage data-pageid="redirect-apple">
<IonHeader>
<IonToolbar>
<IonTitle>Apple</IonTitle>
</IonToolbar>
</IonHeader>
<IonContent>
<TestDescription>
Go to the Orange tab, then press a redirect button. After the redirect only the Apple outlet should be
visible.
</TestDescription>
</IonContent>
</IonPage>
);
};

const OrangePage: React.FC = () => {
const router = useIonRouter();
return (
<IonPage data-pageid="redirect-orange">
<IonHeader>
<IonToolbar>
<IonTitle>Orange</IonTitle>
</IonToolbar>
</IonHeader>
<IonContent>
<IonButton
id="redirect-0ms"
onClick={() => router.push('/redirect-stale-commit/banana/redirect?delay=0', 'forward', 'push')}
>
Redirect after 0 ms
</IonButton>
<IonButton
id="redirect-10ms"
onClick={() => router.push('/redirect-stale-commit/banana/redirect?delay=10', 'forward', 'push')}
>
Redirect after 10 ms
</IonButton>
<IonButton
id="redirect-manual"
onClick={() => router.push('/redirect-stale-commit/banana/redirect', 'forward', 'push')}
>
Go to Banana (redirect manually)
</IonButton>
</IonContent>
</IonPage>
);
};

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 <Navigate replace to="/redirect-stale-commit/apple" />;
}
return (
<IonPage data-pageid="redirect-banana">
<IonContent>
<IonButton id="redirect-now" onClick={() => router.push('/redirect-stale-commit/apple', 'none', 'replace')}>
Redirect now
</IonButton>
</IonContent>
</IonPage>
);
};

const RedirectStaleCommit: React.FC = () => {
return (
<IonTabs>
<IonRouterOutlet id="redirect-tabs">
<Route
path="apple/*"
element={
<IonRouterOutlet ionPage id="apple-outlet">
<Route index element={<ApplePage />} />
</IonRouterOutlet>
}
/>
<Route
path="orange/*"
element={
<IonRouterOutlet ionPage id="orange-outlet">
<Route index element={<OrangePage />} />
</IonRouterOutlet>
}
/>
<Route
path="banana/*"
element={
<IonRouterOutlet ionPage id="banana-outlet">
<Route path="redirect" element={<RedirectToApple />} />
</IonRouterOutlet>
}
/>
</IonRouterOutlet>
<IonTabBar slot="bottom">
<IonTabButton id="tab-apple" tab="apple" href="/redirect-stale-commit/apple">
Apple
</IonTabButton>
<IonTabButton id="tab-orange" tab="orange" href="/redirect-stale-commit/orange">
Orange
</IonTabButton>
</IonTabBar>
</IonTabs>
);
};

export default RedirectStaleCommit;
Original file line number Diff line number Diff line change
@@ -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<void>((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/);
});
});