Skip to content

Commit

Permalink
Issue: wrong pathname encoding (#390)
Browse files Browse the repository at this point in the history
* 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`
  • Loading branch information
sveta-slepner authored Jul 14, 2020
1 parent 0469494 commit e203fe6
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 1 deletion.
7 changes: 6 additions & 1 deletion src/lib/history.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,14 @@ let getLocation = source => {
const url = new URL(href);
pathname = url.pathname;
}

const encodedPathname = pathname
.split("/")
.map(pathPart => encodeURIComponent(decodeURIComponent(pathPart)))
.join("/");

return {
pathname: encodeURI(decodeURI(pathname)),
pathname: encodedPathname,
search,
hash,
href,
Expand Down
16 changes: 16 additions & 0 deletions src/lib/history.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,22 @@ describe("createHistory", () => {

expect(history.location.pathname).toEqual("/p%C3%A5ge");
});

it("should not encode location pathname if it is already encoded", () => {
const mockSource = {
history: {},
location: {
pathname: "/%2F",
search: "",
hash: ""
}
};

const history = createHistory(mockSource);

expect(history.location.pathname).toEqual("/%2F");
});

});

describe("navigate", () => {
Expand Down

0 comments on commit e203fe6

Please sign in to comment.