Skip to content

Commit e203fe6

Browse files
Issue: wrong pathname encoding (#390)
* Issue: wrong pathname encoding The encoding on pathname fails when the URL contains `%2F`, and returns the pathname with `%252F` instead. this is because `encodeURI(decodeURI('%2F')) === "%252F"` Also see: https://codesandbox.io/s/reach-router-encoding-issue-pofyo * fix: Encoded pathname Encoding can now handle side cases where URLs include both encoded and decoded parts that `encode/decodeURI` can't handle, such as `/folder/a%2Fb`
1 parent 0469494 commit e203fe6

File tree

2 files changed

+22
-1
lines changed

2 files changed

+22
-1
lines changed

src/lib/history.js

+6-1
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,14 @@ let getLocation = source => {
1515
const url = new URL(href);
1616
pathname = url.pathname;
1717
}
18+
19+
const encodedPathname = pathname
20+
.split("/")
21+
.map(pathPart => encodeURIComponent(decodeURIComponent(pathPart)))
22+
.join("/");
1823

1924
return {
20-
pathname: encodeURI(decodeURI(pathname)),
25+
pathname: encodedPathname,
2126
search,
2227
hash,
2328
href,

src/lib/history.test.js

+16
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,22 @@ describe("createHistory", () => {
5757

5858
expect(history.location.pathname).toEqual("/p%C3%A5ge");
5959
});
60+
61+
it("should not encode location pathname if it is already encoded", () => {
62+
const mockSource = {
63+
history: {},
64+
location: {
65+
pathname: "/%2F",
66+
search: "",
67+
hash: ""
68+
}
69+
};
70+
71+
const history = createHistory(mockSource);
72+
73+
expect(history.location.pathname).toEqual("/%2F");
74+
});
75+
6076
});
6177

6278
describe("navigate", () => {

0 commit comments

Comments
 (0)