Skip to content

href replaces splats #13593

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
May 12, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions .changeset/unlucky-pigs-chew.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
---
"react-router": patch
---

href replaces splats `*`

```ts
const a = href("/products/*", { "*": "/1/edit" });
// -> /products/1/edit
```
4 changes: 4 additions & 0 deletions packages/react-router/__tests__/href-test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,10 @@ describe("href", () => {
);
});

it("works with splats", () => {
expect(href("/a/*", { "*": "b/c" })).toBe("/a/b/c");
});

it("throws when required params are missing", () => {
expect(() => href("/a/:b")).toThrow(
`Path '/a/:b' requires param 'b' but it was not provided`
Expand Down
4 changes: 4 additions & 0 deletions packages/react-router/lib/href.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,10 @@ export function href<Path extends keyof Args>(
return path
.split("/")
.map((segment) => {
if (segment === "*") {
return params ? params["*"] : undefined;
}

const match = segment.match(/^:([\w-]+)(\?)?/);
if (!match) return segment;
const param = match[1];
Expand Down