Skip to content

Commit 1e023fb

Browse files
committed
Conditionally wait for the render depending on the router state.
1 parent acf0082 commit 1e023fb

File tree

3 files changed

+27
-10
lines changed

3 files changed

+27
-10
lines changed

dev-packages/e2e-tests/test-applications/react-create-browser-router-lazy-routes/tests/transactions.test.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,5 +74,10 @@ test('Captures a navigation transaction', async ({ page }) => {
7474
}),
7575
);
7676

77-
expect(transactionEvent.spans).toEqual([]);
77+
expect(transactionEvent.spans).toEqual([
78+
expect.objectContaining({
79+
op: 'resource.script',
80+
origin: 'auto.resource.browser.metrics',
81+
}),
82+
]);
7883
});

packages/react/src/reactrouterv6-compat-utils.tsx

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -95,19 +95,26 @@ export function createV6CompatibleWrapCreateBrowserRouter<
9595

9696
router.subscribe((state: RouterState) => {
9797
if (state.historyAction === 'PUSH' || state.historyAction === 'POP') {
98-
// Use requestAnimationFrame to wait for Suspense boundaries to settle
99-
requestAnimationFrame(() => {
100-
// Small delay to allow for Suspense transitions
101-
setTimeout(() => {
98+
// Wait for the next render if loading an unsettled route
99+
if (state.navigation.state !== 'idle') {
100+
requestAnimationFrame(() => {
102101
handleNavigation({
103102
location: state.location,
104103
routes,
105104
navigationType: state.historyAction,
106105
version,
107106
basename,
108107
});
109-
}, 100);
110-
});
108+
});
109+
} else {
110+
handleNavigation({
111+
location: state.location,
112+
routes,
113+
navigationType: state.historyAction,
114+
version,
115+
basename,
116+
});
117+
}
111118
}
112119
});
113120

packages/react/src/types.ts

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -182,10 +182,14 @@ export interface RouterInit {
182182
hydrationData?: HydrationState;
183183
}
184184

185+
export type NavigationState = {
186+
state: 'idle' | 'loading' | 'submitting';
187+
}
188+
185189
export type NavigationStates = {
186-
Idle: any;
187-
Loading: any;
188-
Submitting: any;
190+
Idle: NavigationState;
191+
Loading: NavigationState;
192+
Submitting: NavigationState;
189193
};
190194

191195
export type Navigation = NavigationStates[keyof NavigationStates];
@@ -202,6 +206,7 @@ export declare enum HistoryAction {
202206
export interface RouterState {
203207
historyAction: Action | HistoryAction | any;
204208
location: Location;
209+
navigation: Navigation;
205210
}
206211
export interface Router<TState extends RouterState = RouterState> {
207212
state: TState;

0 commit comments

Comments
 (0)