Skip to content

Commit 58d421f

Browse files
authored
Fix other code paths for resolveTo from a splat route (#11045)
* Fix other code paths for resolveTo from a splat route * Bump bundle
1 parent 5f530a7 commit 58d421f

File tree

8 files changed

+54
-18
lines changed

8 files changed

+54
-18
lines changed

.changeset/resolve-to-splat.md

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
---
2+
"react-router": patch
3+
"@remix-run/router": patch
4+
---
5+
6+
Fix bug with `resolveTo` in splat routes
7+
8+
- This is a follow up to [#10983](https://github.com/remix-run/react-router/pull/10983) to handle the few other code paths using `getPathContributingMatches`
9+
- This removes the `UNSAFE_getPathContributingMatches` export from `@remix-run/router` since we no longer need this in the `react-router`/`react-router-dom` layers

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,7 @@
110110
},
111111
"filesize": {
112112
"packages/router/dist/router.umd.min.js": {
113-
"none": "49.3 kB"
113+
"none": "49.4 kB"
114114
},
115115
"packages/react-router/dist/react-router.production.min.js": {
116116
"none": "13.9 kB"

packages/react-router/lib/components.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ import {
1414
AbortedDeferredError,
1515
Action as NavigationType,
1616
createMemoryHistory,
17-
UNSAFE_getPathContributingMatches as getPathContributingMatches,
17+
UNSAFE_getResolveToMatches as getResolveToMatches,
1818
UNSAFE_invariant as invariant,
1919
parsePath,
2020
resolveTo,
@@ -281,7 +281,7 @@ export function Navigate({
281281
// StrictMode they navigate to the same place
282282
let path = resolveTo(
283283
to,
284-
getPathContributingMatches(matches).map((match) => match.pathnameBase),
284+
getResolveToMatches(matches),
285285
locationPathname,
286286
relative === "path"
287287
);

packages/react-router/lib/hooks.tsx

Lines changed: 3 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ import {
1818
IDLE_BLOCKER,
1919
Action as NavigationType,
2020
UNSAFE_convertRouteMatchToUiMatch as convertRouteMatchToUiMatch,
21-
UNSAFE_getPathContributingMatches as getPathContributingMatches,
21+
UNSAFE_getResolveToMatches as getResolveToMatches,
2222
UNSAFE_invariant as invariant,
2323
isRouteErrorResponse,
2424
joinPaths,
@@ -197,9 +197,7 @@ function useNavigateUnstable(): NavigateFunction {
197197
let { matches } = React.useContext(RouteContext);
198198
let { pathname: locationPathname } = useLocation();
199199

200-
let routePathnamesJson = JSON.stringify(
201-
getPathContributingMatches(matches).map((match) => match.pathnameBase)
202-
);
200+
let routePathnamesJson = JSON.stringify(getResolveToMatches(matches));
203201

204202
let activeRef = React.useRef(false);
205203
useIsomorphicLayoutEffect(() => {
@@ -311,14 +309,7 @@ export function useResolvedPath(
311309
): Path {
312310
let { matches } = React.useContext(RouteContext);
313311
let { pathname: locationPathname } = useLocation();
314-
315-
// Use the full pathname for the leaf match so we include splat values
316-
// for "." links
317-
let routePathnamesJson = JSON.stringify(
318-
getPathContributingMatches(matches).map((match, idx) =>
319-
idx === matches.length - 1 ? match.pathname : match.pathnameBase
320-
)
321-
);
312+
let routePathnamesJson = JSON.stringify(getResolveToMatches(matches));
322313

323314
return React.useMemo(
324315
() =>

packages/router/__tests__/path-resolution-test.ts

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -138,7 +138,7 @@ describe("path resolution", () => {
138138
path: "*",
139139
},
140140
],
141-
"/foo",
141+
"/foo/bar",
142142
false
143143
);
144144
});
@@ -391,6 +391,21 @@ describe("path resolution", () => {
391391
]);
392392
});
393393

394+
it("from an index route", () => {
395+
assertRoutingToChild([
396+
{
397+
path: "bar",
398+
children: [
399+
{
400+
id: "activeRoute",
401+
index: true,
402+
},
403+
{ path: "baz" },
404+
],
405+
},
406+
]);
407+
});
408+
394409
it("from a dynamic param route", () => {
395410
assertRoutingToChild([
396411
{
@@ -400,6 +415,15 @@ describe("path resolution", () => {
400415
},
401416
]);
402417
});
418+
419+
it("from a splat route", () => {
420+
assertRoutingToChild([
421+
{
422+
id: "activeRoute",
423+
path: "*",
424+
},
425+
]);
426+
});
403427
/* eslint-enable jest/expect-expect */
404428
});
405429

packages/router/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ export {
8787
ErrorResponseImpl as UNSAFE_ErrorResponseImpl,
8888
convertRoutesToDataRoutes as UNSAFE_convertRoutesToDataRoutes,
8989
convertRouteMatchToUiMatch as UNSAFE_convertRouteMatchToUiMatch,
90-
getPathContributingMatches as UNSAFE_getPathContributingMatches,
90+
getResolveToMatches as UNSAFE_getResolveToMatches,
9191
} from "./utils";
9292

9393
export {

packages/router/router.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ import {
4040
convertRouteMatchToUiMatch,
4141
convertRoutesToDataRoutes,
4242
getPathContributingMatches,
43+
getResolveToMatches,
4344
immutableRouteKeys,
4445
isRouteErrorResponse,
4546
joinPaths,
@@ -3340,7 +3341,7 @@ function normalizeTo(
33403341
// Resolve the relative path
33413342
let path = resolveTo(
33423343
to ? to : ".",
3343-
getPathContributingMatches(contextualMatches).map((m) => m.pathnameBase),
3344+
getResolveToMatches(contextualMatches),
33443345
stripBasename(location.pathname, basename) || location.pathname,
33453346
relative === "path"
33463347
);

packages/router/utils.ts

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1145,6 +1145,17 @@ export function getPathContributingMatches<
11451145
);
11461146
}
11471147

1148+
// Return the array of pathnames for the current route matches - used to
1149+
// generate the routePathnames input for resolveTo()
1150+
export function getResolveToMatches<
1151+
T extends AgnosticRouteMatch = AgnosticRouteMatch
1152+
>(matches: T[]) {
1153+
// Use the full pathname for the leaf match so we include splat values for "." links
1154+
return getPathContributingMatches(matches).map((match, idx) =>
1155+
idx === matches.length - 1 ? match.pathname : match.pathnameBase
1156+
);
1157+
}
1158+
11481159
/**
11491160
* @private
11501161
*/

0 commit comments

Comments
 (0)