Skip to content

fix(react): Remove routes from shared set on <Routes> unmount - #22792

Open
logaretm wants to merge 3 commits into
developfrom
awad/js-3213-react-router-v6-integration-allroutes-accumulates-across
Open

fix(react): Remove routes from shared set on <Routes> unmount#22792
logaretm wants to merge 3 commits into
developfrom
awad/js-3213-react-router-v6-integration-allroutes-accumulates-across

Conversation

@logaretm

@logaretm logaretm commented Jul 28, 2026

Copy link
Copy Markdown
Member

Removes a <Routes>'s routes from the module-level allRoutes set when it unmounts, so they can't be matched against a later, unrelated navigation.

The set accumulated every route ever mounted and never removed any. Once two independent routers had each been mounted, matchRoutes ran over the union of both, so a navigation into one could pick up a param name from the other and produce a hybrid transaction name like /bar/:fooId instead of /bar/:barId.

I've taken out the route adding logic from the effect we had and added another one so that we can isolate the add/remove to the same effect to avoid relying on refs that can be fidgity in dev/prod and strict modes.

closes #22782

Will backport to v10

@linear-code

linear-code Bot commented Jul 28, 2026

Copy link
Copy Markdown

JS-3213

logaretm added 2 commits July 29, 2026 08:47
…ent routers

Adds two independent descendant <SentryRoutes> trees (foo/*, bar/*) and a
cross-router client-side navigation. Because allRoutes is a shared module-level
set, navigating into the second router picks up the first router's param name,
producing the hybrid /bar/:fooId instead of /bar/:barId.

This test currently fails, documenting issue #22782.
The module-level `allRoutes` set accumulated every route ever mounted and
never removed any, so once two independent routers had each been mounted,
`matchRoutes` ran over the union of both and could name a transaction with
one router's static segment and another's param (e.g. `/bar/:fooId`).

Removing a `<Routes>`'s routes when it unmounts keeps the set to what is
currently mounted, so stale routes from an unrelated router can't be matched
against a later navigation.

Fixes #22782
@logaretm
logaretm force-pushed the awad/js-3213-react-router-v6-integration-allroutes-accumulates-across branch from 8771f29 to 1e33054 Compare July 29, 2026 12:47
The previous cleanup added routes only during the mount render pass but removed
them on every unmount. Under StrictMode (mount, unmount, remount) the remount
skipped the add, leaving the shared set missing routes and breaking descendant
name reconstruction in cross-usage scenarios.

Add and remove now share one effect lifecycle, so a StrictMode remount re-adds
the routes it removed on the intermediate unmount.
@logaretm
logaretm marked this pull request as ready for review July 29, 2026 15:32
@logaretm
logaretm requested a review from a team as a code owner July 29, 2026 15:32
@logaretm
logaretm requested review from nicohrubec and s1gr1d and removed request for a team July 29, 2026 15:32
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

React Router v6 integration: allRoutes accumulates across independent/unrelated routers, causing incorrect transaction names

1 participant