-
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
Don't create a new history entry when transitioning to the current path #43
Conversation
9e8d642
to
4f03b16
Compare
This feels more semantically correct to me. Just because |
1b5a955
to
b5432e6
Compare
You're probably right. I was thinking with I changed the implementation. Let me know if/how you want this tested. |
Is this PR going to be included? I think it makes more sense than the current default... |
Yes, I'd definitely like to move forward here. Just haven't had time to take a look! @taurose I'm ok with whatever tests you'd like to write here. Maybe we could write a test that only tests hash history and does a Sorry for taking so long to come back around here. |
Guys, maybe add a In my project I've made a I need it to softly "reload" the page (refetch all the routes data) and it would be great to be the part of the core. Here is this middleware if somebody needs it: /**
* Adds `history.refresh()` method that will call `history.listen` handler again with the current location and `REFRESH` action
*
* @param {function} createHistory
* @returns {object} - `history` object
*/
function useRefresh(createHistory) {
return opts => {
const history = createHistory(opts);
if (!history.listen) {
throw new Error('History should provide "listen" method');
}
let currentLocation;
let originalListener;
function listen(listener) {
originalListener = listener;
history.listen(location => {
currentLocation = location;
listener(location);
});
}
function refresh() {
if (!originalListener || !currentLocation) {
return;
}
currentLocation.action = 'REFRESH';
originalListener(currentLocation);
}
return {
...history,
listen,
refresh
};
};
}
module.exports = useRefresh; |
b5432e6
to
d4e36b4
Compare
Just rebased and added a test that checks if @mjackson: Why only test hash history? That behavior affects all histories. I had to do some changes to hash history to not print a warning, but I don't think it makes sense to test this. @th0r: This wouldn't re-execute react-router transition hooks, would it? Either way, I think this should be a separate feature request. |
Looking good :) |
Still waiting for this PR... how can I help, to move this forward? |
d4e36b4
to
adf852f
Compare
No objections here. I was hoping for @mjackson or someone else to take another look :) |
+1 waiting for this PR |
I'm taking a look at this now, @taurose. Thanks for your patience :) |
@@ -114,6 +114,16 @@ function createHistory(options={}) { | |||
return // Transition was interrupted. | |||
|
|||
if (ok) { | |||
// treat PUSH to current path like REPLACE to be consistent with browsers | |||
if (nextLocation.action === PUSH) { | |||
let { pathname, search } = getCurrentLocation() |
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 100% sure this is the right place to do this, but should be ok for now. My concern is for environments where we may not be able to synchronously call getCurrentLocation
, like native, where we have to use AsyncStorage
to store everything.
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 think in that case we'd want to hold on to the current location (or at least the path).
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.
Why is it correct to use getCurrentLocation
here rather than just location
?
Don't create a new history entry when transitioning to the current path
if (nextLocation.action === PUSH) { | ||
let { pathname, search } = getCurrentLocation() | ||
let currentPath = pathname + search | ||
let path = nextLocation.pathname + nextLocation.search |
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.
@mjackson I just realized that I haven't considered location.hash
. Seems like the two lines here should use createPath(location)
instead of concatenation, right?
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 think createPath
is the correct check here. It doesn't account for basename
(and that part is a bit messy honestly in createBrowserHistory
), but basename
isn't going to change on us anyway, so it doesn't matter. That would also fix #43 (comment).
Any chance on a timeline for this getting published to npm? Using |
@taion well, when these changes will be published on npm? It is so hacky to download package directly from github |
@taurose now I am about the |
Oops, disregard what I said earlier - I got confused about which issue I was on. |
Yeah it would be pretty nice to get this released on npm when possible. |
Just released in version 1.13.1. Enjoy. On Fri, Nov 13, 2015 at 2:59 AM Jimmy Jia [email protected] wrote:
|
@taurose Thank you so much! Finally no more annoying messages at console with redux-router and hash history |
Tested on my app too. Working fine, thanks! |
…/history#43) - Fix various table bugs
I have a case where I have a bunch of anchors on a page, and want to record when a user navigates between those anchors. With the change in this PR, it removes that functionality, because now it treats all hash changes on the same page as if they're one single item in the browser history. Is there a way to optionally allow the old functionality for those of us who need it? Alternately, is there a way to force a |
That doesn't sound good. Seems like we should be checking the full computed path instead. Would you mind opening this as a new issue? It looks like a regression introduced here, and it'd be better to track it separately. |
@taurose Do you think you'll have a chance to address the issues? If not, I can amend the code in question. |
@taurose Never mind, I put together a PR with the fix (: |
This PR normalizes the behavior of all histories to not create a new history entry when pushing the path that's already active. Like browsers do with static content.
Currently, all histories create new entries, except
HashHistory
withoutqueryKey
since it's unable to do so. Instead, it'll print a warning which doesn't seem right considering it's a common scenario.I implemented it in the history's
finishTransition
methods to treat these cases like aREPLACE
.location.action
will still bePUSH
though. Alternatively, I think this could be implemented in the base history to changelocation.action
toREPLACE
. That's what I was doing in an older PR.Also related: remix-run/react-router#1031