Skip to content
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

replaceHash triggered by setHash loses history #48

Open
joshkel opened this issue Jan 3, 2013 · 2 comments · May be fixed by #67
Open

replaceHash triggered by setHash loses history #48

joshkel opened this issue Jan 3, 2013 · 2 comments · May be fixed by #67

Comments

@joshkel
Copy link

joshkel commented Jan 3, 2013

If setHash is called, and the resulting changed signal calls replaceHash, then replaceHash replaces the previous history entry instead of the new history entry.

For example: Hash fragment is /, and setHash('/a') is called. Crossroads.js is used to redirect '/a' to '/a/b' (by calling replaceHash). The history after doing this should be '/', '/a/b', but instead, '/' is replaced with '/a/b', so the history is '/a/b', '/a/b'.

It looks like this is caused by setHash and replaceHash updating window.location after calling _registerChange. (setHash's _registerChange triggers replaceHash, and replaceHash replaces window.location before setHash can update window.location.) If they instead updated window.location first, then that would fix this issue (but may raise other issues; I'm not familiar enough with the code and with browser implementations to know).

For now, I'm using this function as a workaround.

function setHashWithPossibleRedirect(hash)
{
    var old_hash = window.location.hash.replace(/^#/, '');
    hasher.changed.active = false;
    hasher.setHash(hash);
    hasher.changed.active = true;
    hasher.changed.dispatch(hash, old_hash);
}
@millermedeiros
Copy link
Owner

Yes, the changed signal is triggered before the browser updates the window.location.hash. The browser hashchange event takes a while to be dispatched so consecutive calls to setHash and getHash would return different values. I decided to normalize the behavior to make sure that getHash would always return the last value passed to setHash.

This is the kind of change that would require a v2.0 release - breaking change and conceptually different.

I will keep this issue open for now since it might be a good idea to change the whole behavior and do a 2.0 release in the future.

@joshkel
Copy link
Author

joshkel commented Jan 3, 2013

Makes sense. Thanks for the info.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants