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

avoid rearrange query parameters #926

Closed
miccycn opened this issue Nov 22, 2016 · 13 comments
Closed

avoid rearrange query parameters #926

miccycn opened this issue Nov 22, 2016 · 13 comments

Comments

@miccycn
Copy link
Contributor

miccycn commented Nov 22, 2016

Avoid rearrange query parameters.
Sorting and rearranging them will cost a new request,
and make some troubles like generating token which is based on query string.

The issue is in:
util/query.js
function stringifyQuery()

unacquainted with jasmine :(

@fnlctrl
Copy link
Member

fnlctrl commented Nov 23, 2016

Please be more specific about the problem you are facing.
What exactly does Sorting and rearranging them will cost a new request mean?
What trouble would be made in generating token which is based on query string?

Query parameters are actually Key-value pairs, so they don't have the notion of 'order'. If your code depends on their order (which they don't have), you're probably doing it wrong.

@NadokaCiel
Copy link

NadokaCiel commented Nov 24, 2016

What trouble would be made in generating token which is based on query string?

For example: when generating token,a full url path is needed.After token generated and return, the third-party platform will verify the consistency of url.
but ios and weixin will automatically add parameter such as "form" or "isapp"
before:
http://vote.timerchina.com/detailInfo?id=1
after share in weixin:
http://vote.timerchina.com/detailInfo?id=1&from=22
after enter page built with vue-router:
http://vote.timerchina.com/detailInfo?from=22&id=1
the user who enter page with this url will failed to pass validation when share it again
since the order doesn't matter,why sort them?
thanks for your attentionO(∩_∩)O

@fnlctrl
Copy link
Member

fnlctrl commented Nov 24, 2016

Yet another example wechat stupidity 又一个中国特色的问题
Vue-router doesn't intentionally sort query params, it's just that it doesn't care about the initial order when re-stringifying them.
In such case where order of url parameters does matter, simply use dynamic segments instead of queries.
http://router.vuejs.org/en/essentials/dynamic-matching.html

@NadokaCiel
Copy link

谢谢您的回答,我知道该怎么调整了。

@miccycn
Copy link
Contributor Author

miccycn commented Nov 28, 2016

That is obviously intention!!!
In unit test :
test/unit/specs/query.spec.js
There is a note:
// sorted alphabetically

About this issue, besides, we discussed a lot in weibo:
http://weibo.com/1219344334/EiKduopBX?type=comment

In such case,we very much regret that we can`t use dynamic segments instead of queries...

@fnlctrl
Copy link
Member

fnlctrl commented Nov 28, 2016

@miccycn Oops, missed that part. Even if vue-router stopped sorting query keys, currently it still can't reliably preserve the initial order. It will have to remember the initial query to do so.

I don't have a weibo account, would you mind posting the discussion here? I'm very curious about the case where dynamic segments can't be used.

@miccycn
Copy link
Contributor Author

miccycn commented Nov 28, 2016

This case is so complicated......
Let me copy our discussion.....

刘巍峰:
vue-router 2 会重排参数顺序,这算 Bug 还算 Feature 啊。微博的短链解析和卡片呈现,是根据 URL 来的,参数顺序变了,URL 就变了,这是个大坑。不光在微博,在微*信内,因为 SDK 的签名是用 URL 算的,所以时机把握不好就跪了。
工程师日常:
被微信的坑过
尤小右:
可以开个 issue
me:
其实就是那个sort()的问题,好奇是出于什么考虑要sort一下。
尤小右:
这个其实是为了防止 Object.keys 返回的顺序各平台可能不一致顺手加了一个... 删了测试能过的话就开 PR 吧

I have tried to delete sort() in util/query.js, and it would be OK like that.

@fnlctrl
Copy link
Member

fnlctrl commented Nov 28, 2016

I don't see any reason dynamic segments cannot be used in the discussion..

Just deleting sort wouldn't be enough, since browsers doesn't loop over objects reliably with the same order.

@miccycn
Copy link
Contributor Author

miccycn commented Nov 28, 2016

删了sort()后,我们测试过中国网民常用浏览器都没什么问题。

可能是我们哪些细节地方还没考虑到。你说的会出问题是比如哪些情况呢?之前因为没重排而出现 issue 的 case 我可以看看么?

动态路由匹配需要更改和加入新的路由,而且由于微博的业务复杂,要加的新路由数量也指数增加,不仅后端要跟着新加,我们之前使用的url也会作废。
如果不是自己当着练手玩、从0开始、啥都自己说了算的项目,
而是从一个复杂项目迁移出来,去新增路由实在不是明智之举。

@fnlctrl
Copy link
Member

fnlctrl commented Nov 28, 2016

就像Evan之前说的一样,各个平台顺序是不能保证的,因为ECMA标准没有规定Object.keys()返回的顺序,全部依靠浏览器实现。{a:1, b:2, c:3}的遍历顺序可以是c, b, a,这是完全合法的。

跟Object一样,query string本身的设计就是键值对,没有顺序这个概念。RFC标准W3C标准都没有规定对于所谓"顺序"的处理。对于当初设计成依赖query“顺序”的已有系统,那实在是比较遗憾。

不过,如果你们只考虑中国网民常用浏览器的话,也倒是可以删掉这个sort。因为大部分人不依赖query的“顺序”(也不应该依赖,任何有顺序的参数都应该使用dynamic segments),所以并不是一个breaking change。但还是要重申一下,这不能保证你的url在所有情况下一定能正常工作。保险的做法是记住上一个query的顺序,下一次序列化的时候按照这个顺序来。总之,欢迎提交PR。

@miccycn
Copy link
Contributor Author

miccycn commented Nov 28, 2016

唉……这没办法,中国最大的两个网络社交媒体平台:微博和微信,都设计成了依赖query顺序了。

但是是否可以这么理解,不sort()是不可靠的,有一定的隐患,同样有一定概率会造成confusion。
sort()使confusion的概率增加了。
如果是这样的话为啥要sort()呢?……

我提交过PR:
#925
但这不是不敢乱动vue-router的jasmine单测么… :-( …所以才来提issue。

@fnlctrl
Copy link
Member

fnlctrl commented Nov 28, 2016

那个测试项删掉就可以了。因为它是针对sort()的测试,不sort了就可以删掉咯。提PR的时候不用担心修改测试的问题,因为都是会经过review的 🙂

唉……这没办法,中国最大的两个网络社交媒体平台:微博和微信,都设计成了依赖query顺序了。

分享个网页还需要拿url去签名真的是中国特色,纯粹是用来恶心开发者的。单从签名的角度上说,url要保持一致(包括query的顺序)是可以理解的。所以为了适应这个需要签名的系统,开发应用的时候url用dynamic segments传参保持顺序是最稳妥的做法。因为query参数谁都可以随便改(比如微信会加个'from'之类的),而且没有标准规定要遵守原来的顺序。

@miccycn
Copy link
Contributor Author

miccycn commented Nov 28, 2016

唉……大坑慎入……
让这隐患暂时先这么隐着,也总好过路由框架多此一举把它提前暴露出来(在不影响其他功能的前提下)……

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

No branches or pull requests

3 participants