Skip to content

Commit

Permalink
Fix matching a nested absolute splat route
Browse files Browse the repository at this point in the history
Fixes REM-333
  • Loading branch information
mjackson committed Oct 2, 2021
1 parent dcf5c09 commit af7d038
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 11 deletions.
25 changes: 20 additions & 5 deletions packages/react-router/__tests__/absolute-path-matching-test.tsx
@@ -1,12 +1,12 @@
import type { RouteObject } from "react-router";
import { matchRoutes } from "react-router";

describe("absolute path matching", () => {
function pickPaths(routes: RouteObject[], pathname: string) {
let matches = matchRoutes(routes, { pathname });
return matches ? matches.map(match => match.route.path || "") : [];
}
function pickPaths(routes: RouteObject[], pathname: string): string[] | null {
let matches = matchRoutes(routes, pathname);
return matches && matches.map(match => match.route.path || "");
}

describe("absolute path matching", () => {
it("matches a nested route with an absolute path", () => {
let routes = [
{
Expand All @@ -26,6 +26,21 @@ describe("absolute path matching", () => {
expect(pickPaths(routes, "/users/123")).toEqual(["/users", "/users/:id"]);
});

it("matches a nested splat route with an absolute path", () => {
let routes = [
{
path: "/users",
children: [{ path: "/users/*" }]
}
];

// expect(pickPaths(routes, "/users")).toEqual(["/users"]);
expect(pickPaths(routes, "/users/not-found")).toEqual([
"/users",
"/users/*"
]);
});

it("throws when the nested path does not begin with its parent path", () => {
expect(() => {
matchRoutes(
Expand Down
9 changes: 3 additions & 6 deletions packages/react-router/index.tsx
Expand Up @@ -903,16 +903,13 @@ function matchRouteBranch<ParamKey extends string = string>(
let matches: RouteMatch[] = [];
for (let i = 0; i < routesMeta.length; ++i) {
let meta = routesMeta[i];
let end = i === routesMeta.length - 1;
let trailingPathname =
matchedPathname === "/"
? pathname
: pathname.slice(matchedPathname.length) || "/";
let match = matchPath(
{
path: meta.relativePath,
caseSensitive: meta.caseSensitive,
end: i === routesMeta.length - 1
},
{ path: meta.relativePath, caseSensitive: meta.caseSensitive, end },
trailingPathname
);

Expand Down Expand Up @@ -1068,7 +1065,7 @@ function compilePath(
});

if (path.endsWith("*")) {
if (path.endsWith("/*")) {
if (path !== "/*" && path.endsWith("/*")) {
source += "(?:\\/(.+)|\\/?)$"; // Don't include the / in params['*']
} else {
source += "(.*)$";
Expand Down

0 comments on commit af7d038

Please sign in to comment.