From 62bd43f76b56e6f7010ad5671355954226fda3bf Mon Sep 17 00:00:00 2001 From: ShaneK Date: Wed, 11 Mar 2026 10:44:55 -0700 Subject: [PATCH] fix(react-router): sort fallback params by specificity to prevent less-specific overwrites --- .../src/ReactRouter/ReactRouterViewStack.tsx | 16 ++++++++++++++-- 1 file changed, 14 insertions(+), 2 deletions(-) diff --git a/packages/react-router/src/ReactRouter/ReactRouterViewStack.tsx b/packages/react-router/src/ReactRouter/ReactRouterViewStack.tsx index d3dc8e537f..efb57b96c3 100644 --- a/packages/react-router/src/ReactRouter/ReactRouterViewStack.tsx +++ b/packages/react-router/src/ReactRouter/ReactRouterViewStack.tsx @@ -89,7 +89,7 @@ const getFallbackParamsFromViewItems = ( currentOutletId: string, currentPathname: string ): RouteParams => { - const params: RouteParams = {}; + const matchingViews: { params: RouteParams; pathLength: number }[] = []; for (const otherViewItem of allViewItems) { if (otherViewItem.outletId === currentOutletId) continue; @@ -98,11 +98,23 @@ const getFallbackParamsFromViewItems = ( if (otherMatch?.params && Object.keys(otherMatch.params).length > 0) { const matchedPathname = otherMatch.pathnameBase || otherMatch.pathname; if (matchedPathname && currentPathname.startsWith(matchedPathname)) { - Object.assign(params, otherMatch.params); + matchingViews.push({ + params: otherMatch.params, + pathLength: matchedPathname.length, + }); } } } + // Sort ascending by path length so more-specific (longer) paths are applied + // last and their params take priority over less-specific ones. + matchingViews.sort((a, b) => a.pathLength - b.pathLength); + + const params: RouteParams = {}; + for (const view of matchingViews) { + Object.assign(params, view.params); + } + return params; };