From 10d3f94daa36dbb8c57d68642674830bbf9fe768 Mon Sep 17 00:00:00 2001 From: ShaneK Date: Mon, 9 Nov 2026 11:43:46 -0800 Subject: [PATCH 1/2] fix(react-router): register IonPage with the view that rendered it, not a stale pathname match --- .../src/ReactRouter/ReactRouterViewStack.tsx | 24 +++- .../src/ReactRouter/StackManager.tsx | 11 +- packages/react-router/test/base/src/App.tsx | 6 + .../react-router/test/base/src/pages/Main.tsx | 6 + .../pages/stale-register/StaleRegister.tsx | 130 ++++++++++++++++++ .../e2e/playwright/stale-register.spec.ts | 38 +++++ packages/react/src/routing/StackContext.tsx | 6 +- 7 files changed, 216 insertions(+), 5 deletions(-) create mode 100644 packages/react-router/test/base/src/pages/stale-register/StaleRegister.tsx create mode 100644 packages/react-router/test/base/tests/e2e/playwright/stale-register.spec.ts diff --git a/packages/react-router/src/ReactRouter/ReactRouterViewStack.tsx b/packages/react-router/src/ReactRouter/ReactRouterViewStack.tsx index c9c72558087..23e48d06670 100644 --- a/packages/react-router/src/ReactRouter/ReactRouterViewStack.tsx +++ b/packages/react-router/src/ReactRouter/ReactRouterViewStack.tsx @@ -6,7 +6,7 @@ */ import type { RouteInfo, ViewItem } from '@ionic/react'; -import { generateId, IonRoute, ViewLifeCycleManager, ViewStacks } from '@ionic/react'; +import { generateId, IonRoute, StackContext, ViewLifeCycleManager, ViewStacks } from '@ionic/react'; import React from 'react'; import type { PathMatch } from 'react-router'; import { Navigate, UNSAFE_RouteContext as RouteContext } from 'react-router-dom'; @@ -267,6 +267,26 @@ const ViewRouteContextProvider: React.FC<{ return {children}; }; +/** + * Passes the view that rendered a page to `registerIonPage`. A page that mounts + * during a navigation registers with the previous pathname, so the path lookup + * alone can pick the wrong view. + */ +const ViewStackContextProvider: React.FC<{ + viewItem: ViewItem; + children: React.ReactNode; +}> = ({ viewItem, children }) => { + const parent = React.useContext(StackContext); + const value = React.useMemo( + () => ({ + ...parent, + registerIonPage: (page: HTMLElement, routeInfo: RouteInfo) => parent.registerIonPage(page, routeInfo, viewItem), + }), + [parent, viewItem] + ); + return {children}; +}; + export class ReactRouterViewStack extends ViewStacks { /** * Stores the computed parent path for each outlet. @@ -640,7 +660,7 @@ export class ReactRouterViewStack extends ViewStacks { removeView={() => this.remove(viewItem)} > - {componentElement} + {componentElement} ); diff --git a/packages/react-router/src/ReactRouter/StackManager.tsx b/packages/react-router/src/ReactRouter/StackManager.tsx index 456f14f2b8c..8ab592787fa 100644 --- a/packages/react-router/src/ReactRouter/StackManager.tsx +++ b/packages/react-router/src/ReactRouter/StackManager.tsx @@ -1270,8 +1270,9 @@ export class StackManager extends React.PureComponent { * * @param page The element of the rendered ``. * @param routeInfo The route information that associates with ``. + * @param ownerViewItem The view item that rendered ``, if known. */ - registerIonPage(page: HTMLElement, routeInfo: RouteInfo) { + registerIonPage(page: HTMLElement, routeInfo: RouteInfo, ownerViewItem?: ViewItem) { /** * DO NOT remove ion-page-invisible here. * @@ -1291,7 +1292,13 @@ export class StackManager extends React.PureComponent { } this.pendingPageTransition = false; - const foundView = this.context.findViewItemByRouteInfo(routeInfo, this.id); + const pathView = this.context.findViewItemByRouteInfo(routeInfo, this.id); + /** + * The lookup can use a stale pathname (see `ViewStackContextProvider`), so + * prefer the view that rendered the page when the two disagree. + */ + const foundView = + pathView && ownerViewItem?.outletId === this.id && pathView !== ownerViewItem ? ownerViewItem : pathView; if (foundView) { const oldPageElement = foundView.ionPageElement; diff --git a/packages/react-router/test/base/src/App.tsx b/packages/react-router/test/base/src/App.tsx index f130c119338..983e79bb6cf 100644 --- a/packages/react-router/test/base/src/App.tsx +++ b/packages/react-router/test/base/src/App.tsx @@ -61,6 +61,7 @@ import OutletUnmountBeforeReady from './pages/outlet-unmount-before-ready/Outlet import { PropsUpdateDirect, PropsUpdateRoutesWrapper } from './pages/props-update/PropsUpdate'; 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'; const App: React.FC = () => { @@ -133,6 +134,11 @@ const App: React.FC = () => { } /> } /> } /> + } /> + } + /> } /> } /> diff --git a/packages/react-router/test/base/src/pages/Main.tsx b/packages/react-router/test/base/src/pages/Main.tsx index f6c14236275..ebde2e85d41 100644 --- a/packages/react-router/test/base/src/pages/Main.tsx +++ b/packages/react-router/test/base/src/pages/Main.tsx @@ -160,6 +160,12 @@ const Main: React.FC = () => { Wildcard No Heuristic + + Stale Register (catch-all) + + + Stale Register (no catch-all) + Splat Sibling diff --git a/packages/react-router/test/base/src/pages/stale-register/StaleRegister.tsx b/packages/react-router/test/base/src/pages/stale-register/StaleRegister.tsx new file mode 100644 index 00000000000..f14bf637e9a --- /dev/null +++ b/packages/react-router/test/base/src/pages/stale-register/StaleRegister.tsx @@ -0,0 +1,130 @@ +import { + IonButton, + IonContent, + IonHeader, + IonPage, + IonRouterOutlet, + IonTitle, + IonToolbar, + useIonRouter, +} from '@ionic/react'; +import React, { createContext, useContext, useState } from 'react'; +import { Route } from 'react-router-dom'; + +import TestDescription from '../../components/TestDescription'; + +/** + * Logging out clears the user and navigates in the same click, so the Join page + * mounts before the outlet's route info catches up. It should still be shown. + */ + +interface AuthState { + loggedIn: boolean; + setLoggedIn: (loggedIn: boolean) => void; +} + +const AuthContext = createContext({ loggedIn: false, setLoggedIn: () => undefined }); + +const GuardAnonOnly: React.FC<{ children: React.ReactNode }> = ({ children }) => { + const { loggedIn } = useContext(AuthContext); + return loggedIn ? null : <>{children}; +}; + +const Join: React.FC<{ base: string }> = ({ base }) => { + const { setLoggedIn } = useContext(AuthContext); + const router = useIonRouter(); + + return ( + + + + Join + + + + { + setLoggedIn(true); + router.push(`${base}/verify`, 'forward'); + }} + > + Log in + + + Log in, then log out. Log out clears the user and navigates back here in the same click, so this page mounts + in the same commit as the navigation. It should be shown, not left hidden behind a blank screen. + + + + ); +}; + +const Verify: React.FC<{ base: string }> = ({ base }) => { + const { setLoggedIn } = useContext(AuthContext); + const router = useIonRouter(); + + return ( + + + + Verify + + + + { + setLoggedIn(false); + router.navigateRoot(`${base}/join`); + }} + > + Log out + + + + ); +}; + +const NotFound: React.FC = () => ( + + Not found + +); + +const StaleRegister: React.FC<{ base: string; catchAll: boolean }> = ({ base, catchAll }) => { + const [loggedIn, setLoggedIn] = useState(false); + const guardedJoin = ( + + + + ); + + return ( + + + } /> + + {/* The outlet crashes on a `false` child, so each variant gets its own outlet. */} + {catchAll ? ( + + + } /> + + ) : ( + + + + )} + + } + /> + + + ); +}; + +export default StaleRegister; diff --git a/packages/react-router/test/base/tests/e2e/playwright/stale-register.spec.ts b/packages/react-router/test/base/tests/e2e/playwright/stale-register.spec.ts new file mode 100644 index 00000000000..d8becfb0577 --- /dev/null +++ b/packages/react-router/test/base/tests/e2e/playwright/stale-register.spec.ts @@ -0,0 +1,38 @@ +import { test } from '@playwright/test'; + +import { ionPageVisible, withTestingMode } from './utils/test-utils'; + +/** + * A page that mounts during a navigation should stay with the view that rendered + * it, even when a sibling catch-all matched the previous pathname. + * + * The variant without a catch-all doesn't reproduce the bug. It's there because + * always using the owning view broke that layout. + */ +const ISSUE = 'https://github.com/ionic-team/ionic-framework/issues/31696'; + +for (const { base, label } of [ + { base: '/stale-register', label: 'with a catch-all sibling' }, + { base: '/stale-register-no-catch-all', label: 'without a catch-all sibling' }, +]) { + test.describe(`page that mounts during navigation, ${label}`, () => { + test('should show the join page after each logout', async ({ page }, testInfo) => { + testInfo.annotations.push({ type: 'issue', description: ISSUE }); + + await page.goto(withTestingMode(`${base}/join`)); + await ionPageVisible(page, 'stale-register-join'); + + for (let i = 0; i < 3; i++) { + await page.locator('[data-testid="log-in"]').click(); + await ionPageVisible(page, 'stale-register-verify'); + + await page.locator('[data-testid="log-out"]').click(); + await ionPageVisible(page, 'stale-register-join'); + } + + // The page used to be hidden a render after it appeared, so check it stays visible. + await page.waitForTimeout(500); + await ionPageVisible(page, 'stale-register-join'); + }); + }); +} diff --git a/packages/react/src/routing/StackContext.tsx b/packages/react/src/routing/StackContext.tsx index 02694c36e52..4996c0ae820 100644 --- a/packages/react/src/routing/StackContext.tsx +++ b/packages/react/src/routing/StackContext.tsx @@ -1,9 +1,13 @@ import React from 'react'; import type { RouteInfo } from '../models/RouteInfo'; +import type { ViewItem } from './ViewItem'; export interface StackContextState { - registerIonPage: (page: HTMLElement, routeInfo: RouteInfo) => void; + /** + * @param ownerViewItem The view that rendered the page, if the router knows it. + */ + registerIonPage: (page: HTMLElement, routeInfo: RouteInfo, ownerViewItem?: ViewItem) => void; isInOutlet: () => boolean; } From bb548e0c07fe2fc7efab32f3914bdf5c70847cf2 Mon Sep 17 00:00:00 2001 From: ShaneK Date: Mon, 9 Nov 2026 11:52:50 -0800 Subject: [PATCH 2/2] style(react): add blank line between type imports in StackContext --- packages/react/src/routing/StackContext.tsx | 1 + 1 file changed, 1 insertion(+) diff --git a/packages/react/src/routing/StackContext.tsx b/packages/react/src/routing/StackContext.tsx index 4996c0ae820..5d08f30d60c 100644 --- a/packages/react/src/routing/StackContext.tsx +++ b/packages/react/src/routing/StackContext.tsx @@ -1,6 +1,7 @@ import React from 'react'; import type { RouteInfo } from '../models/RouteInfo'; + import type { ViewItem } from './ViewItem'; export interface StackContextState {