-
Notifications
You must be signed in to change notification settings - Fork 961
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
Prevent pushing duplicate paths to the history API. #558
Conversation
Okay, this is a little bigger... HashHistory emits a warning about replacing instead of pushing to the stack. But this is actually the WHATWG standards behaviour. So should I remove the warning from the HashHistory and homogenise the implementation around BrowserHistory, MemoryHistory & HashHistory? |
modules/createBrowserHistory.js
Outdated
@@ -165,6 +165,11 @@ const createBrowserHistory = (props = {}) => { | |||
const action = "PUSH" | |||
const location = createLocation(path, state, createKey(), history.location) | |||
|
|||
if (createPath(location) === createPath(history.location)) { |
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.
You're only comparing the pathname in this case. What about the search/querystring? Since you're doing this in multiple histories, you should export a common function, like locationsAreEqual
.
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.
Great tip, thanks.
There's already a locationsAreEqual
method, which is great. But it also checks for key
, something we don't want. Should I add an override to the method?
That would result in the following signature:
locationsAreEqual = (a, b, ignoreKey) => { ... }
Sounds reasonable if you ask me. On the other hand, invoking the function gets a bit vague that way:
locationsAreEqual(location, oldLocation, true)
I could also create a new method locationsExceptKeyAreEqual
or locationEqualsDestination
. What do you think?
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.
Hmm, I'm not sure why key is in there at all. If the key is equal, the locations are equal. That's the point of the key value being in there. So, that function would appear to be fundamentally flawed. There's only one internal usage:
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, I see why. Hash histories don't use key. I haven't been following those specifics too carefully, since I don't use those myself.
I would add a locationsAreEquivalent
function in this case that checks pathname
and search
are equal.
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.
Thanks for thinking along!
Looks like the key prop could be removed from the compare function, right? As hash history doesn't use the key prop anyhow. Then it would be usable in all places without adding an extra method.
0796a45
to
e3206fb
Compare
Rewrote history a little. Not too smart for open-source, oops. |
I see that semi-colons have been enabled in Prettier, I'll fix the conflicts. |
Waiting for #559 before I can fix the conflicts in this branch. |
@timdorr Ready for review again! PS: What about the HashHistory warning? |
subscribing to this as mentioned in #507 (comment) |
What do you mean @guidobouman? It's not possible to push the same path using hash history the way it is currently implemented. Notice how the second |
I should also note that using If I understand you correctly, @guidobouman, are you saying it shouldn't? |
Perhaps we should fix this higher up in React Router's |
Moving the discussion on this to #470, there's more historical info there. |
Based on the discussion in #470 I don't think we want to change the behavior of I'm going to close this PR, but thank you for taking the time to push this discussion forward @guidobouman :) |
Always, @mjackson! PS: I added some small bug fixes / tweaks in this PR, I'll put them up as different PR's. |
Fixes #507