Skip to content

Commit

Permalink
fix: Allow routes starting with @ in layout routes (#8877)
Browse files Browse the repository at this point in the history
  • Loading branch information
theostavrides committed May 18, 2022
1 parent b82809f commit 08b7523
Show file tree
Hide file tree
Showing 4 changed files with 70 additions and 1 deletion.
1 change: 1 addition & 0 deletions contributors.yml
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@
- shamsup
- shihanng
- shivamsinghchahar
- theostavrides
- thisiskartik
- ThornWu
- timdorr
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -184,6 +184,32 @@ describe("Descendant <Routes> splat matching", () => {
</div>
`);
});
it("allows `@` to appear at the beginning", () => {
let renderer = renderNestedSplatRoute([
"/courses/react/@react-fundamentals",
]);
expect(renderer.toJSON()).toMatchInlineSnapshot(`
<div>
<h1>
Courses
</h1>
<div>
<h1>
React
</h1>
<div>
<h1>
React Fundamentals
</h1>
<p>
The params are
{"*":"@react-fundamentals","splat":"@react-fundamentals"}
</p>
</div>
</div>
</div>
`);
});
it("allows url-encoded entities to appear at the beginning", () => {
let renderer = renderNestedSplatRoute([
"/courses/react/%20react-fundamentals",
Expand Down
42 changes: 42 additions & 0 deletions packages/react-router/__tests__/layout-routes-test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,48 @@ describe("A layout route", () => {
</h1>
`);
});

it("allows routes starting with `@`", () => {
let renderer: TestRenderer.ReactTestRenderer;
TestRenderer.act(() => {
renderer = TestRenderer.create(
<MemoryRouter initialEntries={["/@splat"]}>
<Routes>
<Route
element={
<div>
<h1>Layout</h1>
<Outlet />
</div>
}
>
<Route
path="*"
element={
<div>
<h1>Splat</h1>
</div>
}
/>
</Route>
</Routes>
</MemoryRouter>
);
});

expect(renderer.toJSON()).toMatchInlineSnapshot(`
<div>
<h1>
Layout
</h1>
<div>
<h1>
Splat
</h1>
</div>
</div>
`);
});
describe("matches when a nested splat route begins with a special character", () => {
it("allows routes starting with `-`", () => {
let renderer: TestRenderer.ReactTestRenderer;
Expand Down
2 changes: 1 addition & 1 deletion packages/react-router/lib/router.ts
Original file line number Diff line number Diff line change
Expand Up @@ -477,7 +477,7 @@ function compilePath(
// Additionally, allow paths starting with `.`, `-`, `~`, and url-encoded entities,
// but do not consume the character in the matched path so they can match against
// nested paths.
"(?:(?=[.~-]|%[0-9A-F]{2})|\\b|\\/|$)";
"(?:(?=[@.~-]|%[0-9A-F]{2})|\\b|\\/|$)";
}

let matcher = new RegExp(regexpSource, caseSensitive ? undefined : "i");
Expand Down

0 comments on commit 08b7523

Please sign in to comment.