Skip to content

Commit cb3489f

Browse files
author
ChenLi
committed
Fix encoded # in path parameter causes route to be evaluated differently
1 parent 7372aff commit cb3489f

File tree

3 files changed

+42
-0
lines changed

3 files changed

+42
-0
lines changed

contributors.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -282,3 +282,4 @@
282282
- yuleicul
283283
- zeromask1337
284284
- zheng-chuang
285+
- dunnai

packages/react-router-dom/__tests__/navigate-encode-params-test.tsx

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,4 +93,44 @@ describe("navigate with params", () => {
9393
expect(node.innerHTML).toMatch(/react\+router/);
9494
});
9595
});
96+
97+
describe("when navigate params are encoded using #", () => {
98+
it("the encoding of the # parameter in the URL has been changed", () => {
99+
function Start() {
100+
let navigate = useNavigate();
101+
102+
React.useEffect(() => {
103+
navigate("/route/react%23router/subroute/router/blog");
104+
});
105+
106+
return null;
107+
}
108+
109+
function Blog() {
110+
let params = useParams();
111+
return <h1>Blog: {params.routeName}-{params.subrouteName}</h1>;
112+
}
113+
114+
act(() => {
115+
ReactDOM.createRoot(node).render(
116+
<BrowserRouter>
117+
<Routes>
118+
<Route path="/" element={<Start />} />
119+
<Route path="/route/:routeName/subroute/:subrouteName/*" element={
120+
<Routes>
121+
<Route path="blog" element={<Blog />} />
122+
<Route path="*" element={null} />
123+
</Routes>
124+
} />
125+
</Routes>
126+
</BrowserRouter>
127+
);
128+
});
129+
130+
let pathname = window.location.pathname;
131+
expect(pathname).toEqual("/route/react%23router/subroute/router/blog");
132+
133+
expect(node.innerHTML).toMatch(/react#router-router/);
134+
});
135+
});
96136
});

packages/router/history.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -694,6 +694,7 @@ function getUrlBasedHistory(
694694
// pre-encode them since they might be part of a matching splat param from
695695
// an ancestor route
696696
href = href.replace(/ $/, "%20");
697+
href = typeof to === "string" ? href.replace(/#/, "%23") : href
697698
invariant(
698699
base,
699700
`No window.location.(origin|href) available to create URL for href: ${href}`

0 commit comments

Comments
 (0)