Skip to content

Commit

Permalink
Fix encoded # in path parameter causes route to be evaluated differently
Browse files Browse the repository at this point in the history
  • Loading branch information
ChenLi committed Nov 30, 2024
1 parent 72f9af9 commit 5cc0999
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 0 deletions.
1 change: 1 addition & 0 deletions contributors.yml
Original file line number Diff line number Diff line change
Expand Up @@ -309,3 +309,4 @@
- yuleicul
- zeromask1337
- zheng-chuang
- dunnai
Original file line number Diff line number Diff line change
Expand Up @@ -93,4 +93,44 @@ describe("navigate with params", () => {
expect(node.innerHTML).toMatch(/react\+router/);
});
});

describe("when navigate params are encoded using #", () => {
it("the encoding of the # parameter in the URL has been changed", () => {
function Start() {
let navigate = useNavigate();

React.useEffect(() => {
navigate("/route/react%23router/subroute/router/blog");
});

return null;
}

function Blog() {
let params = useParams();
return <h1>Blog: {params.routeName}-{params.subrouteName}</h1>;
}

act(() => {
ReactDOM.createRoot(node).render(
<BrowserRouter>
<Routes>
<Route path="/" element={<Start />} />
<Route path="/route/:routeName/subroute/:subrouteName/*" element={
<Routes>
<Route path="blog" element={<Blog />} />
<Route path="*" element={null} />
</Routes>
} />
</Routes>
</BrowserRouter>
);
});

let pathname = window.location.pathname;
expect(pathname).toEqual("/route/react%23router/subroute/router/blog");

expect(node.innerHTML).toMatch(/react#router-router/);
});
});
});
1 change: 1 addition & 0 deletions packages/react-router/lib/router/history.ts
Original file line number Diff line number Diff line change
Expand Up @@ -695,6 +695,7 @@ function getUrlBasedHistory(
// pre-encode them since they might be part of a matching splat param from
// an ancestor route
href = href.replace(/ $/, "%20");
href = typeof to === "string" ? href.replace(/#/, "%23") : href
invariant(
base,
`No window.location.(origin|href) available to create URL for href: ${href}`
Expand Down

0 comments on commit 5cc0999

Please sign in to comment.