From 2e079d491a72fa260c464ecd30092fee10145723 Mon Sep 17 00:00:00 2001 From: Matt Brophy Date: Thu, 30 Oct 2025 14:16:13 -0400 Subject: [PATCH 1/2] Fix race condition with rendering and layout effect subscription during hydration --- packages/react-router/lib/components.tsx | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/packages/react-router/lib/components.tsx b/packages/react-router/lib/components.tsx index 1f20af4099..a86da605ec 100644 --- a/packages/react-router/lib/components.tsx +++ b/packages/react-router/lib/components.tsx @@ -667,6 +667,16 @@ export function RouterProvider({ // pick up on any render-driven redirects/navigations (useEffect/) React.useLayoutEffect(() => router.subscribe(setState), [router, setState]); + // Track race conditions where we finish initializing prior to the layout + // effect above running to register our listener. If we manually detect a + // change in `state.initialized`, automatically sync state. + let initialized = state.initialized; + React.useLayoutEffect(() => { + if (!initialized && router.state.initialized) { + logErrorsAndSetState(router.state); + } + }, [initialized, logErrorsAndSetState, router.state]); + // When we start a view transition, create a Deferred we can use for the // eventual "completed" render React.useEffect(() => { From c1fe1fc397002fcf300dee4095de177032325cf7 Mon Sep 17 00:00:00 2001 From: Matt Brophy Date: Mon, 3 Nov 2025 11:08:58 -0500 Subject: [PATCH 2/2] Add changeset --- .changeset/calm-crabs-bathe.md | 5 +++++ 1 file changed, 5 insertions(+) create mode 100644 .changeset/calm-crabs-bathe.md diff --git a/.changeset/calm-crabs-bathe.md b/.changeset/calm-crabs-bathe.md new file mode 100644 index 0000000000..89f3cc7dff --- /dev/null +++ b/.changeset/calm-crabs-bathe.md @@ -0,0 +1,5 @@ +--- +"react-router": patch +--- + +Fix a potential race condition that can occur when rendering a `HydrateFallback` and initial loaders land before the `router.subscribe` call happens in the `RouterProvider` layout effect