-
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
Disable automatic pathname encoding #461
Comments
The issue seems to only be when the developer wants to include reserved characters ( I think that a To be honest, I'm having a hard time understanding why the hash symbol is being encoded in the example. Is it just so that the value can be access as |
@pshrmn the fact that the path encoding is automatic and cannot be disabled or worked around is unexpected and makes this library incompatible with the browser API which it abstracts. I hope the code above demonstrates this issue, and I think this is a bug, not a feature. The larger issue is that this behavior blocks valid use cases in a higher-level library such as
To be crystal clear, I am not using using hashes at all. So how do I go about this? Here is what I tried: <Link to="/view/#XYZ" />
<!-- Expected and actual --> <a href="/view/#XYZ" /> Looks fine, but I need the ID value in the path, not the hash. <Link to="/view/%23XYZ" />
<!-- Expected --> <a href="/view/%23XYZ" />
<!-- Actual --> <a href="/view/%2523XYZ" /> This is how a URL would be specified for Let's specify <Link to={{pathname: '/view/#XYZ'}} />
<!-- Expected --> <a href="/view/%23XYZ" />
<!-- Actual --> <a href="/view/#XYZ" /> Again, not the expected result. <Link to={{pathname: '/view/%23XYZ'}} />
<!-- Expected --> <a href="/view/%23XYZ" />
<!-- Actual --> <a href="/view/%2523XYZ" /> What am I missing? |
When // before
<Route path='/%E6%AD%B4%E5%8F%B2' ... />
// after
<Route path='/歴史' ... /> What I was proposing was that for people who want to handle encoding/decoding themselves, history would provide a const history = createBrowserHistory({ raw: true }); In raw mode, |
Actually, @nil4 you're really only concerned with the encoding, right? Is the automatic decoding breaking anything for you? I don't actually think that |
@pshrmn I think you're right. The only thing we need to be doing is decoding, not encoding. The browser will handle that for us (like it was before we made the change!) |
Okay, so I tested out what happens if just // no encoding
history.pushState(null, null, '/99% of the time')
// window.location.pathname === "/99%%20of%20the%20time"
decodeURI(window.location.pathname) // throws a URIError
// encoding
history.pushState(null, null, encodeURI('/99% of the time'))
// window.location.pathname === "/99%25%20of%20the%20time"
decodeURI(window.location.pathname) // "/99% of the time" It makes sense that a percent sign is not auto-encoded (should As far as possible solutions go, these include:
history.push({ pathname: '/99% of the time' }) // "/99%25%20of%20the%20time"
history.push({ pathname: '/%20abc' }, null, true) // "/%20abc" In theory, there should also be the ability to not decode the pathname. The only time that I have seen this requested had to do with a validation string made up of random characters, which could include percent signs. I'm sure that there are other use cases, though, so we should also consider what to do with that. |
Correct me if I'm wrong but could another option be for us to just remove the history.push('/99% of the time') // throws "Your URL cannot safely be decoded. Please encode it properly first" EDIT: We can detect invalid URLs by passing them straight to |
@mjackson I've got a branch that I was putting together, but hadn't submitted because of the percent issue. It doesn't have the warning if the path is bad, but otherwise should be good. |
But do you think that's a good solution, @pshrmn? To |
I'll need to give it some more thought, but I think that this makes sense. 99.9% (:smirk:) of the time, users aren't going to run into this issue. When they do, the solution is as simple as manually encoding with <Link to={{ pathname: encodeURI('/60%+of+the+time') }}>By Odeon</Link> This has me wondering now about what should happen when a Edit: for example https://reacttraining.com/react-router/web/guides/quick-start% |
That same concern was brought up in #448. I had thought it was resolved but maybe not. If I try putting random % into well-known URLs around the web, everyone handles them differently!
Lots of sites are OK with the borked URLs, so I'm not 100% sure what the difference is. It may just be different libraries that tolerate the % better than others. Maybe in these cases we just warn and leave the URL encoded? Isn't that what people would want most of the time? |
In any case I should say that I think the idea to throw when someone tries to |
I came across this same issue because a URL parameter I use in contains a forward slash, which I encode myself. I upgraded to React Router v4 and new history packages, and now it gets double encoded. If I leave the forward slash untouched, it is not encoded at all, which messes up my URL parameters. I think the 'raw' option proposed here will fix my issue, as I can keep encoding the URL myself. Or am I missing an obvious fix? |
Check. That would work. Thanks! |
This is fixed in #465 |
In #442 and #445 automatic encoding of
pathname
was added. This change breaks my application and I could not find a good workaround except reverting tohistory@4.5.0
.The app uses react-router and URLs that look like
/view/:id
, whereid
s can contains#
and other special characters (e.g. a link to id#abc
is encoded<Link to="/view/%23abc" />
). With history@4.5.0 and earlier, everything worked fine; the app takes care to correctly encode theid
and links work.After upgrading to v4.6.0 and v4.6.1, when clicking the link
/view/%23abc
(or when usinghistory.push
on the same URL), the browser address bar unexpectedly shows/view/%2523abc
. Note the double encoding of the#
character.Below is a standalone repro of the issue, comparing
pushState
with history@4.6.1push
, againstwindow.location.pathname
.To run it, save the file locally, then
npm i http-server && node node_modules/.bin/http-server -a 127.0.0.1 -p 8080
.In the console output, note the double encoding of
#
when usinghistory.push
:When the first
<script>
is changed to referencehistory@4.5.0
the behavior is correct:Please reconsider the automatic encoding approach, as applications cannot work around it. It should be left to the application to encode the URL correctly before passing it to this library.
The text was updated successfully, but these errors were encountered: