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? |
| const action = "PUSH" | ||
| const location = createLocation(path, state, createKey(), history.location) | ||
|
|
||
| if (createPath(location) === createPath(history.location)) { |
There was a problem hiding this comment.
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.
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.
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.
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.
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