Skip to content

Commit d47f0e7

Browse files
Kostiantyn Popovychbrophdawg11
andauthored
fix: generating path when passed only slash and star (#9174)
* [#8883] Add tests for generate path func executing with slash and splat * [#8883] Fix generating path when only slash + star passed * Signed CLA by adding KostiantynPopovych to contributors.yml * [#8883] Increase allowed boundle size * Update approach Co-authored-by: Matt Brophy <matt@brophy.org>
1 parent 9a5ec31 commit d47f0e7

File tree

3 files changed

+38
-3
lines changed

3 files changed

+38
-3
lines changed

contributors.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,4 +81,5 @@
8181
- fz6m
8282
- TkDodo
8383
- infoxicator
84+
- KostiantynPopovych
8485
- CanRau

packages/react-router/__tests__/generatePath-test.tsx

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ describe("generatePath", () => {
1717
"/courses/routing/grades"
1818
);
1919
expect(generatePath("*", { "*": "routing/grades" })).toBe(
20-
"/routing/grades"
20+
"routing/grades"
2121
);
2222
});
2323
});
@@ -42,4 +42,31 @@ describe("generatePath", () => {
4242
expect(generatePath("/courses/*", {})).toBe("/courses");
4343
});
4444
});
45+
46+
it("throws only on on missing named parameters, but not missing splat params", () => {
47+
expect(() => generatePath(":foo")).toThrow();
48+
expect(() => generatePath("/:foo")).toThrow();
49+
expect(() => generatePath("*")).not.toThrow();
50+
expect(() => generatePath("/*")).not.toThrow();
51+
});
52+
53+
it("only interpolates and does not add slashes", () => {
54+
expect(generatePath("*")).toBe("");
55+
expect(generatePath("/*")).toBe("/");
56+
57+
expect(generatePath("foo*")).toBe("foo");
58+
expect(generatePath("/foo*")).toBe("/foo");
59+
60+
expect(generatePath(":foo", { foo: "bar" })).toBe("bar");
61+
expect(generatePath("/:foo", { foo: "bar" })).toBe("/bar");
62+
63+
expect(generatePath("*", { "*": "bar" })).toBe("bar");
64+
expect(generatePath("/*", { "*": "bar" })).toBe("/bar");
65+
66+
expect(generatePath("foo:bar", { bar: "baz" })).toBe("foobaz");
67+
expect(generatePath("/foo:bar", { bar: "baz" })).toBe("/foobaz");
68+
69+
expect(generatePath("foo*", { "*": "bar" })).toBe("foobar");
70+
expect(generatePath("/foo*", { "*": "bar" })).toBe("/foobar");
71+
});
4572
});

packages/router/utils.ts

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -485,10 +485,17 @@ export function generatePath<Path extends string>(
485485
invariant(params[key] != null, `Missing ":${key}" param`);
486486
return params[key]!;
487487
})
488-
.replace(/\/*\*$/, (_) => {
488+
.replace(/(\/?)\*/, (_, prefix, __, str) => {
489489
const star = "*" as PathParam<Path>;
490490

491-
return params[star] == null ? "" : params[star].replace(/^\/*/, "/");
491+
if (params[star] == null) {
492+
// If no splat was provided, trim the trailing slash _unless_ it's
493+
// the entire path
494+
return str === "/*" ? "/" : "";
495+
}
496+
497+
// Apply the splat
498+
return `${prefix}${params[star]}`;
492499
});
493500
}
494501

0 commit comments

Comments
 (0)