-
Notifications
You must be signed in to change notification settings - Fork 140
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
Sort URLSearchParams prior to stringification #26
Comments
I suggest allowing constructor to take an I think this would be a nice and convenient opt-in. |
Since URLSearchParams with a different order is actually different semantically, it would make more sense to have something like |
@domenic should that be similar to |
Hmm I'm not sure. If you did want to allow a custom comparator, I'd imagine something like this? const newUsp = usp.sorted(([k1, v1], [k2, v2]) => v1.localeCompare(v2)); // sort by values You could start with just a no-arg |
Yeah probably.
Less clear to me, as there's no good default unless you just assume all keys can be converted to strings.
Yeah, it would be good to see some library code with at least moderate use before we try to bake this in... |
So looking at this somewhat fresh and rereading the tweet thread it seems this is actually specific to URLSearchParams since it affects cache interactions. Neither Headers nor FormData encounter such a scenario. It also seems that a simple lexical sort should be sufficient here and that you actually do want to affect the current instance. @igrigorik does that sound about right? If that is correct, just adding a |
@igrigorik @jeffposnick ping. |
👍 to the suggestion at #26 (comment) |
If this were to happen, having the new The reason for the default sort not affecting the value order is that ordering of the values can be significant. We should not assume that it's ok to move the order of the values around. |
@annevk yep, another 👍 for #26 (comment). |
@jasnell I was hoping to go with the minimum viable solution here. If you need a callback, you might as well write a quick |
I can live with that |
See whatwg/url#26 for context.
This method is added to increase cache hits when making requests. It’s opt-in as the order of code points in a URL’s query is significant by default. It’s up to applications to decide if name order is not significant for them. Tests: web-platform-tests/wpt#4531. Fixes #26.
Please make sure the sort is stable, so that the pseudo-array format |
@pornel are you saying that the proposed language fails to capture that? Also, if you have good tests, they'd be appreciated. |
I just realized that in my first comment I'd said the the sort need not be stable, that was a typo. I meant that it should. The language in the pr looks good to me. |
Ah, the PR is fine. |
How do people feel about my suggestion of a const sorted = (new URLSearchParams(usp)).sort(); I suppose. |
@domenic I think one big problem with that is that it doesn't work well if you got to the |
Yeah, I guess that makes sense; I forgot there wasn't a |
Maybe just include an example in the spec of how to do a non-destructive sort like my above. |
See whatwg/url#26 for context.
This method is added to increase cache hits when making requests. It’s opt-in as the order of code points in a URL’s query is significant by default. It’s up to applications to decide if name order is not significant for them. Tests: web-platform-tests/wpt#4531. Fixes #26.
So close to all ending in 4. |
Fixes: nodejs#10760 Ref: whatwg/url#26 Ref: whatwg/url#199 Ref: web-platform-tests/wpt#4531
PR-URL: #11098 Fixes: #10760 Ref: whatwg/url#26 Ref: whatwg/url#199 Ref: web-platform-tests/wpt#4531 Reviewed-By: James M Snell <[email protected]> Reviewed-By: Joyee Cheung <[email protected]>
PR-URL: #11098 Fixes: #10760 Ref: whatwg/url#26 Ref: whatwg/url#199 Ref: web-platform-tests/wpt#4531 Reviewed-By: James M Snell <[email protected]> Reviewed-By: Joyee Cheung <[email protected]>
Backport-of: nodejs#11098 Fixes: nodejs#10760 Ref: whatwg/url#26 Ref: whatwg/url#199 Ref: web-platform-tests/wpt#4531
Backport-of: nodejs#11098 Fixes: nodejs#10760 Ref: whatwg/url#26 Ref: whatwg/url#199 Ref: web-platform-tests/wpt#4531
Backport-of: #11098 Fixes: #10760 Ref: whatwg/url#26 Ref: whatwg/url#199 Ref: web-platform-tests/wpt#4531
Inspired by this tweet, I'd argue that the specified behavior for the
URLSearchParams
stringifier should be changed to either automatically sort by default (unlikely, due to @annevk's objections), or alternatively to support some sort of boolean opt-in to enable sorting.I'd imagine that sorting based on parameter names, falling back to parameter values in the case of repeated parameters, would make the most sense.
The text was updated successfully, but these errors were encountered: