react-router icon indicating copy to clipboard operation
react-router copied to clipboard

Should hash should come after query, not before?

Open davidtheclark opened this issue 11 years ago • 11 comments

(This only applies when using hash URLs, instead of HTML5 history. I assume none of this would be a problem using history instead of hash.)

The URI RFC says that hash fragments should come after query params, not before. However, if I use React Router to set a URL, it does the opposite. For example, if a component that uses the Navigation mixin runs this.replaceWith('user', null, {showAge: true});, I will get the URL mydomain.com/page/#/user?showAge=true --- but it seems from the RFC that this should be mydomain.com/page?showAge=true/#/user.

The backend of my app (Spring) understands if the param comes before the hash, but not the other way around --- so the URL generated by using replaceWith cannot be used by the backend. That's a problem for me, because I can't then reuse that URL such that if I go directly to it the backend will understand the query and send the right data.

Am I confused about this? Should React Router's current behavior change?

davidtheclark avatar Jan 08 '15 01:01 davidtheclark

Ah, that reminds me of something similar that had been done in Backbone and that would break an app in old browser versions that could only use hash-based routing. My app was using query params that were supposed to be used on the server side, but those "old" browsers wouldn't send them as they were "transferred" to the hash fragment by Backbone.

Browsers will not send hash fragments to the server, meaning that this behavior literally "breaks the internet", much more severely than Kim Kardashian presumably did recently! My feeling is that lib authors such as those from Backbone took liberties in this "let's all use Javascript and build heavy clients" era to bring some features more easily and have forgotten or decided to forget the fact that there are still servers behind those websites. As far as Backbone is concerned, I was told that it would not change because "much effort" had been put in providing the feature that introduced that "bug" (to me, it is one) and that "many people wanted it". I really wish people here think otherwise...

Query params are designed and specified to be sent to the website's hosting server and thus to precede the hash fragment. I'd say any client-side routers should/must comply to this specification.

olance avatar Jan 08 '15 14:01 olance

https://github.com/rackt/react-router/issues/636#issuecomment-67984275

ryanflorence avatar Jan 08 '15 15:01 ryanflorence

:+1:

olance avatar Jan 08 '15 15:01 olance

Thanks @rpflorence . My use case is that the client router doesn't actually use the query params, but I want to keep the URL up-to-date such that it could be copied and then pasted into a new browser tab and the server would know how to handle it. So what would be nice for me is if I had the option in methods like .replaceWith() to populate the URL such that it could be an "entry URL" (as you call it in your comment).

Does that make sense?

davidtheclark avatar Jan 08 '15 15:01 davidtheclark

I don't think it's even possible to change the url (except hash) without history API or causing a full page reload, or is it?

agundermann avatar Jan 08 '15 19:01 agundermann

I believe you're right @taurose; I didn't think of that. Maybe then there's no way for me to achieve the goal of keeping the URL up-to-date through client-side routing so that the URL could always be re-used for server-side queries. Bummer.

davidtheclark avatar Jan 08 '15 19:01 davidtheclark

I still don't have my head wrapped around what you're trying to do, but it sounds like you need to just make your server work with HistoryLocation

ryanflorence avatar Jan 08 '15 19:01 ryanflorence

I think what I want to do is exposing a limitation of client-side hash-based routing --- a limitation that as you say wouldn't exist with HistoryLocation. I'll attempt again to explain what I'm trying, to make sure you agree that this is a limitation, not something can be fixed, and to try to make the issue useful for others running into the problem:

I'm using hash routing for IE8 compatibility. I have two pages: a form and a table. The information in the table, its order and content, is based on either the info you entered into the form or (if you arrived at the table directly, instead of going through the form) the query params in your URL. So I want to be able to go straight to the table's route, with query params in the URL, and have the server know how what data to put on the page (I am not using React Router on the server ... it is SpringMVC). For that to happen, it seems that the server needs the hash fragment after the query, so I'd need a URL like /domain.com/app?sortOrder=asc&sortProperty=foo&category=bar/#/table.

Ideally, if the user progressed through the form, instead, when they arrive at the table route the URL would be updated to match the format the server would require to go directly to that table (i.e. what they entered in the form would be translated to a query, and that query would be placed in front of the hash) --- because that way the user would be able to copy the URL, close the browser, and paste the URL in the future to arrive directly at the table of results without having to fill out the form again.

davidtheclark avatar Jan 08 '15 20:01 davidtheclark

This is precisely why HistoryLocation falls back to RefreshLocation. You can't have the same urls work for hash and history. My recommendation for IE8 support is to just use HistoryLocation and let IE8 fall back to RefreshLocation. I'm sure you can write enough code on the client and server to make it all work, but the router isn't going to help out much. (Like, read in the query on the client from before the hash, then replaceWith to get it after the hash as well, etc.)

ryanflorence avatar Jan 08 '15 20:01 ryanflorence

Thanks for the ideas.

davidtheclark avatar Jan 08 '15 20:01 davidtheclark

(you'll still end up with duplicate urls, which is why the router doesn't attempt to do anything here)

ryanflorence avatar Jan 08 '15 20:01 ryanflorence