-
-
Notifications
You must be signed in to change notification settings - Fork 10.3k
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
Fix issues with useFormAction/useResolvedPath for dot paths in param/splat routes #10983
Changes from all commits
b696de3
3ada561
85b30e3
1618835
6deb430
78d5566
60b47e2
22a3657
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
--- | ||
"react-router": patch | ||
--- | ||
|
||
Fix bug in `useResolvedPath` that would cause `useResolvedPath(".")` in a splat route to lose the splat portion of the URL path. | ||
|
||
- ⚠️ This fixes a quite long-standing bug specifically for `"."` paths inside a splat route which incorrectly dropped the splat portion of the URL. If you are relative routing via `"."` inside a splat route in your application you should double check that your logic is not relying on this buggy behavior and update accordingly. |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2653,6 +2653,46 @@ function testDomRouter( | |
"/foo/bar" | ||
); | ||
}); | ||
|
||
it("does not include dynamic parameters from a parent layout route", async () => { | ||
let router = createTestRouter( | ||
createRoutesFromElements( | ||
<Route path="/"> | ||
<Route path="foo" element={<ActionEmptyComponent />}> | ||
<Route path=":param" element={<h1>Param</h1>} /> | ||
</Route> | ||
</Route> | ||
), | ||
{ | ||
window: getWindow("/foo/bar"), | ||
} | ||
); | ||
let { container } = render(<RouterProvider router={router} />); | ||
|
||
expect(container.querySelector("form")?.getAttribute("action")).toBe( | ||
"/foo" | ||
); | ||
}); | ||
|
||
it("does not include splat parameters from a parent layout route", async () => { | ||
let router = createTestRouter( | ||
createRoutesFromElements( | ||
<Route path="/"> | ||
<Route path="foo" element={<ActionEmptyComponent />}> | ||
<Route path="*" element={<h1>Splat</h1>} /> | ||
</Route> | ||
</Route> | ||
), | ||
{ | ||
window: getWindow("/foo/bar/baz/qux"), | ||
} | ||
); | ||
let { container } = render(<RouterProvider router={router} />); | ||
|
||
expect(container.querySelector("form")?.getAttribute("action")).toBe( | ||
"/foo" | ||
); | ||
}); | ||
}); | ||
|
||
describe("index routes", () => { | ||
|
@@ -2876,6 +2916,44 @@ function testDomRouter( | |
"/foo/bar" | ||
); | ||
}); | ||
|
||
it("includes param portion of path when no action is specified (inline splat)", async () => { | ||
let router = createTestRouter( | ||
createRoutesFromElements( | ||
<Route path="/"> | ||
<Route path="foo"> | ||
<Route path=":param" element={<NoActionComponent />} /> | ||
</Route> | ||
</Route> | ||
), | ||
{ | ||
window: getWindow("/foo/bar"), | ||
} | ||
); | ||
let { container } = render(<RouterProvider router={router} />); | ||
|
||
expect(container.querySelector("form")?.getAttribute("action")).toBe( | ||
"/foo/bar" | ||
); | ||
}); | ||
|
||
it("includes splat portion of path when no action is specified (nested splat)", async () => { | ||
let router = createTestRouter( | ||
createRoutesFromElements( | ||
<Route path="/"> | ||
<Route path="foo/:param" element={<NoActionComponent />} /> | ||
</Route> | ||
), | ||
{ | ||
window: getWindow("/foo/bar"), | ||
} | ||
); | ||
let { container } = render(<RouterProvider router={router} />); | ||
|
||
expect(container.querySelector("form")?.getAttribute("action")).toBe( | ||
"/foo/bar" | ||
); | ||
}); | ||
}); | ||
|
||
describe("splat routes", () => { | ||
|
@@ -2895,7 +2973,7 @@ function testDomRouter( | |
let { container } = render(<RouterProvider router={router} />); | ||
|
||
expect(container.querySelector("form")?.getAttribute("action")).toBe( | ||
"/foo?a=1" | ||
"/foo/bar?a=1" | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This existing test was asserting the buggy behavior. When no |
||
); | ||
}); | ||
|
||
|
@@ -2915,7 +2993,7 @@ function testDomRouter( | |
let { container } = render(<RouterProvider router={router} />); | ||
|
||
expect(container.querySelector("form")?.getAttribute("action")).toBe( | ||
"/foo" | ||
"/foo/bar" | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This existing test was asserting the buggy behavior. When |
||
); | ||
}); | ||
|
||
|
@@ -2935,7 +3013,25 @@ function testDomRouter( | |
let { container } = render(<RouterProvider router={router} />); | ||
|
||
expect(container.querySelector("form")?.getAttribute("action")).toBe( | ||
"/foo" | ||
"/foo/bar" | ||
); | ||
}); | ||
|
||
it("includes splat portion of path when no action is specified (inline splat)", async () => { | ||
let router = createTestRouter( | ||
createRoutesFromElements( | ||
<Route path="/"> | ||
<Route path="foo/*" element={<NoActionComponent />} /> | ||
</Route> | ||
), | ||
{ | ||
window: getWindow("/foo/bar/baz"), | ||
} | ||
); | ||
let { container } = render(<RouterProvider router={router} />); | ||
|
||
expect(container.querySelector("form")?.getAttribute("action")).toBe( | ||
"/foo/bar/baz" | ||
); | ||
}); | ||
}); | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -530,7 +530,7 @@ describe("<Link> href", () => { | |
}); | ||
|
||
expect(renderer.root.findByType("a").props.href).toEqual( | ||
"/inbox/messages" | ||
"/inbox/messages/abc" | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Same as above - this asserted the buggy behavior |
||
); | ||
}); | ||
|
||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
New tests we didn't have before to ensure that the
useResolvedPath
changes don't make us accidentally pick up child route portions