Skip to content
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

sortQueryParameters encodes the query string #149

Closed
freearhey opened this issue Aug 11, 2021 · 5 comments · Fixed by #158
Closed

sortQueryParameters encodes the query string #149

freearhey opened this issue Aug 11, 2021 · 5 comments · Fixed by #158

Comments

@freearhey
Copy link

Is it supposed to be like this?

sortQueryParameters: true

normalizeUrl('http://sindresorhus.com/?url=http://example.com', {
    sortQueryParameters: true
});
//=> 'http://sindresorhus.com/?url=http%3A%2F%2Fexample.com'

sortQueryParameters: false

normalizeUrl('http://sindresorhus.com/?url=http://example.com', {
    sortQueryParameters: false
});
//=> 'http://sindresorhus.com/?url=http://example.com'
@freearhey freearhey changed the title sortQueryParameters encodes the query string sortQueryParameters encodes the query string Aug 11, 2021
@ludofischer
Copy link
Contributor

That's unforunately expected since normalize-url uses URL.searchParams() under the hood which follows different rules from the IETF URI spec since it is based on the WHATWG URL spec https://nodejs.org/api/url.html#urlsearchparams.

@freearhey
Copy link
Author

Got it.

@freearhey
Copy link
Author

why closed.

because my question was answered

how to resolve

pass the output string through the decodeURIComponent():

decodeURIComponent('http://sindresorhus.com/?url=http%3A%2F%2Fexample.com') 
// => 'http://sindresorhus.com/?url=http://example.com'

@ludofischer
Copy link
Contributor

pass the output string through the decodeURIComponent():

decodeURIComponent('http://sindresorhus.com/?url=http%3A%2F%2Fexample.com') 
// => 'http://sindresorhus.com/?url=http://example.com'

Wow! Good find. But what if the original URL parameters were encoded?

@ludofischer
Copy link
Contributor

I've made a PR to always decode the parameters, since after all the package name starts with normalize- :-)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants