-
Notifications
You must be signed in to change notification settings - Fork 958
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
No encode, decode in createLocation #465
Conversation
@@ -21,6 +21,7 @@ const execSteps = (steps, history, done) => { | |||
if (index === steps.length) | |||
cleanup() | |||
} catch (error) { | |||
console.log('caught error', error) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Did you wanna leave this in?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Oh bother. That's the remnant of my debugging when I accidentally passed done
to a describe
call instead of an it
call.
}, | ||
(location) => { | ||
expect(location).toMatch({ | ||
pathname: '/view/#abc' |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm not actually sure about this test case. It passes, but I'm not sure if the behavior is desirable. This only happens if the user passes a bad location object.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Seems like the kind of thing we don't actually need to test. i.e. if anyone actually tried to do this I'd probably just tell them to use hash
instead of putting it on the pathname
.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I went ahead and added two encoding checks. My thoughts are:
Throwing when given a bad path but also creating locations from bad paths is inconsistent. We should probably just choose one.
I was curious how Express deals with bad percent-encoding and when I used a wildcard path, it failed, but with no path it worked. I think that users would typically provide a pattern to match against, so that would be in favor or throwing an error.
// when requesting /test%
app.use('*', function() {...}) // throws URIError
app.use(function() {...}) // did not throw
RFC 3986 2.4 states:
Because the percent ("%") character serves as the indicator for
percent-encoded octets, it must be percent-encoded as "%25" for that
octet to be used as data within a URI.
That, too, seems to indicate that we should treat a bad percent-encoding as an error.
modules/LocationUtils.js
Outdated
location.pathname = decodeURI(location.pathname) | ||
} catch (e) { | ||
if (e instanceof URIError) { | ||
warning( |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If we try to create a location with a bad percent-encoding, we swallow the error, keep location.pathname
encoded, and log a warning.
modules/PathUtils.js
Outdated
throw new Error( | ||
'You are attempting to create a path that cannot be decoded. ' + | ||
'Please call encodeURI on the pathname.' | ||
) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
A slightly more useful error than the default Uncaught URIError: URI malformed
Updated to just throw in Internally, |
The obvious issue with try {
ReactDOM.render((
<BrowserRouter>
<App />
</BrowserRouter
), holder)
} catch (e) {
ReactDOM.render((
<MalformedURIMessage />
), holder)
} I still think that |
@pshrmn I think this is the right approach; an app using an invalid/malformed path can always be fixed to encode correctly. Thank you for fixing this. |
Just bumping this since it has been a while. Whether or not this is the best approach, something definitely should be done here since the current implementation (made by myself |
@pshrmn I would love to see this added. 👍 |
This looks good to me. Thanks, @pshrmn! |
Seems like this should probably be a patch release since we're just fixing a bug we introduced in 4.6.0. Would you agree? |
I agree. More of a bug fix than "added functionality", so I don't think that it would warrant a minor bump. |
As a minor addendum, the next release would also include #459, but that is also pretty much just a bug fix, so I don't think that it warrants a minor bump. |
One more thing I just remembered is that #485 needs to be addressed before the next release. |
OK, just got that one in for you. @mjackson Did you want to push out a 4.6.2 release or add me to npm so I can take care of that for you? (you probably should add Ryan, BTW 😄) |
Thanks, @timdorr. I'll go ahead and cut the release :) |
Removes the automatic encoding of
pathname
s, leaving that up to the browser.Moves the decoding to
createLocation
because 1) that is now used by thegetDOMLocation
calls to create a location and 2) we should ensure that if a user pushes a location whosepathname
is encoded, we decode it for consistency.