Skip to content
Merged
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
24 changes: 22 additions & 2 deletions packages/react-router/src/ReactRouter/ReactRouterViewStack.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -267,6 +267,26 @@ const ViewRouteContextProvider: React.FC<{
return <RouteContext.Provider value={heldValue}>{children}</RouteContext.Provider>;
};

/**
* 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 <StackContext.Provider value={value}>{children}</StackContext.Provider>;
};

export class ReactRouterViewStack extends ViewStacks {
/**
* Stores the computed parent path for each outlet.
Expand Down Expand Up @@ -640,7 +660,7 @@ export class ReactRouterViewStack extends ViewStacks {
removeView={() => this.remove(viewItem)}
>
<ViewRouteContextProvider value={routeContextValue} hold={holdRouteContext}>
{componentElement}
<ViewStackContextProvider viewItem={viewItem}>{componentElement}</ViewStackContextProvider>
</ViewRouteContextProvider>
</ViewLifeCycleManager>
);
Expand Down
11 changes: 9 additions & 2 deletions packages/react-router/src/ReactRouter/StackManager.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -1270,8 +1270,9 @@ export class StackManager extends React.PureComponent<StackManagerProps> {
*
* @param page The element of the rendered `<IonPage>`.
* @param routeInfo The route information that associates with `<IonPage>`.
* @param ownerViewItem The view item that rendered `<IonPage>`, if known.
*/
registerIonPage(page: HTMLElement, routeInfo: RouteInfo) {
registerIonPage(page: HTMLElement, routeInfo: RouteInfo, ownerViewItem?: ViewItem) {
/**
* DO NOT remove ion-page-invisible here.
*
Expand All @@ -1291,7 +1292,13 @@ export class StackManager extends React.PureComponent<StackManagerProps> {
}
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;
Expand Down
6 changes: 6 additions & 0 deletions packages/react-router/test/base/src/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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 = () => {
Expand Down Expand Up @@ -133,6 +134,11 @@ const App: React.FC = () => {
<Route path="/props-update-routes/*" element={<PropsUpdateRoutesWrapper />} />
<Route path="/props-update-direct/*" element={<PropsUpdateDirect />} />
<Route path="/splat-sibling/*" element={<SplatSibling />} />
<Route path="/stale-register/*" element={<StaleRegister base="/stale-register" catchAll />} />
<Route
path="/stale-register-no-catch-all/*"
element={<StaleRegister base="/stale-register-no-catch-all" catchAll={false} />}
/>
<Route path="/index-sibling/*" element={<IndexSibling />} />
<Route path="/empty-path-sibling/*" element={<EmptyPathSibling />} />
</IonRouterOutlet>
Expand Down
6 changes: 6 additions & 0 deletions packages/react-router/test/base/src/pages/Main.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,12 @@ const Main: React.FC = () => {
<IonItem routerLink="/wildcard-no-heuristic">
<IonLabel>Wildcard No Heuristic</IonLabel>
</IonItem>
<IonItem routerLink="/stale-register/join">
<IonLabel>Stale Register (catch-all)</IonLabel>
</IonItem>
<IonItem routerLink="/stale-register-no-catch-all/join">
<IonLabel>Stale Register (no catch-all)</IonLabel>
</IonItem>
<IonItem routerLink="/splat-sibling">
<IonLabel>Splat Sibling</IonLabel>
</IonItem>
Expand Down
Original file line number Diff line number Diff line change
@@ -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<AuthState>({ 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 (
<IonPage data-pageid="stale-register-join">
<IonHeader>
<IonToolbar>
<IonTitle>Join</IonTitle>
</IonToolbar>
</IonHeader>
<IonContent>
<IonButton
data-testid="log-in"
onClick={() => {
setLoggedIn(true);
router.push(`${base}/verify`, 'forward');
}}
>
Log in
</IonButton>
<TestDescription>
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.
</TestDescription>
</IonContent>
</IonPage>
);
};

const Verify: React.FC<{ base: string }> = ({ base }) => {
const { setLoggedIn } = useContext(AuthContext);
const router = useIonRouter();

return (
<IonPage data-pageid="stale-register-verify">
<IonHeader>
<IonToolbar>
<IonTitle>Verify</IonTitle>
</IonToolbar>
</IonHeader>
<IonContent>
<IonButton
data-testid="log-out"
onClick={() => {
setLoggedIn(false);
router.navigateRoot(`${base}/join`);
}}
>
Log out
</IonButton>
</IonContent>
</IonPage>
);
};

const NotFound: React.FC = () => (
<IonPage data-pageid="stale-register-not-found">
<IonContent>Not found</IonContent>
</IonPage>
);

const StaleRegister: React.FC<{ base: string; catchAll: boolean }> = ({ base, catchAll }) => {
const [loggedIn, setLoggedIn] = useState(false);
const guardedJoin = (
<GuardAnonOnly>
<Join base={base} />
</GuardAnonOnly>
);

return (
<AuthContext.Provider value={{ loggedIn, setLoggedIn }}>
<IonRouterOutlet id="stale-register-outer">
<Route path="verify" element={<Verify base={base} />} />
<Route
path="*"
element={
<IonPage data-pageid="stale-register-shell">
{/* The outlet crashes on a `false` child, so each variant gets its own outlet. */}
{catchAll ? (
<IonRouterOutlet id="stale-register-inner">
<Route path="join/*" element={guardedJoin} />
<Route path="*" element={<NotFound />} />
</IonRouterOutlet>
) : (
<IonRouterOutlet id="stale-register-inner">
<Route path="join/*" element={guardedJoin} />
</IonRouterOutlet>
)}
</IonPage>
}
/>
</IonRouterOutlet>
</AuthContext.Provider>
);
};

export default StaleRegister;
Original file line number Diff line number Diff line change
@@ -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');
});
});
}
7 changes: 6 additions & 1 deletion packages/react/src/routing/StackContext.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,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;
}

Expand Down
Loading