-
Notifications
You must be signed in to change notification settings - Fork 960
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
Decode and encode URLs #442
Conversation
One possible issue is that a location object that contains encoded values will be double-encoded when creating a URL. I feel like location objects should only contain decoded values, so I don't think that this is a big issue, but I don't know. |
Makes perfect sense to me, given a URI is the unstructured, serialized string form of a structured location object. We should be encoding when creating a path/href. And it follows that we should also be decoding when parsing that path/href. I'll take a look through the code in more detail now, but this is logically sound. |
I'm not entirely confident in |
Seems like we just tell people using |
Thanks for the PR, @pshrmn :) |
I think that part of why I'm wary about encoding the search string is that React Router otherwise does not care about the search string, so forcing it to be decoded feels out of place. Also, besides Another potential issue is if someone were to have a character that is not encoded by { pathname: '/call', search='?number=#867-5309', hash: '' }
// creates the URL /call?number=#867-5309
{ pathname: '/call', search='?number=', hash: '#867-5309' } If we expected the user to correctly encode their own search strings, then we would not have to worry about that sort of problem. Additionally, I'm not actually sure we need to be encoding any part of the path. Browsers seem to handle that for us perfectly fine. |
I think Given a setup where the query string looks something like I have no way to control the format of the search string once its passed into |
@LukeAskew Why do you need to control the format of the search string? |
I have some utils for formatting the search string, similar to qs. Here's a distilled version of the setup: const history = createBrowserHistory();
// returns `filters[0]=foo&filters[1]=bar`
function stringify(obj) {
const str = doSomeStuff(obj);
return decodeURIComponent(str);
}
function updateQueryString(obj) {
return history.push({ pathname: window.location.pathname, search: `?${stringify(obj)}` });
}
updateQueryString({
filters: ['foo', 'bar'],
}); I don't want |
FWIW I also think that we should only be encoding the pathname (#445). |
When a
history
gets the URL from the browser, the location object created from it should have decoded properties. When ahistory
creates a URL (by calling itscreateHref
method), the URL returned should be encoded. The actual changes to the code are fairly simple.parsePath
callsdecodeURI
andcreatePath
callsencodeURI
.When calling
push
and the path is a string,parsePath
is called bycreateLocation
, so that is how I tested that.createPath
is tested viacreateHref
.In case you were curious, the translations (all in Japanese via Google Translate) are:
"歴史" is "history"
"キー" is "key"
"値" is "value"
"ハッシュ" is "hash"