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

Only handle hashes with a '/' when using createHashHistory #131

Closed
wants to merge 1 commit into from

Conversation

cwrs
Copy link

@cwrs cwrs commented Nov 10, 2015

This PR adds the ability to ignore hash changes when using createHashHistory().

Example:

function ignorePath(path){
   return path.startWidth('ignore-me');
}
let history = createHistory({'ignorePath': ignorePath});

We have the issue, that the hashChangeListener calls ensureSlash for hashes, not handled by the history.

@taion
Copy link
Contributor

taion commented Nov 10, 2015

Can you speak a little to the use case you're trying to handle here?

@cwrs
Copy link
Author

cwrs commented Nov 11, 2015

Our Redux-Application runs within another single-page application (GWT) that handles history via hashes itself. In our setup the outer application only handles specific hashes that start with '!'. When a user of the inner application clicks on a link with a '!' at the beginning to reach a page of the outer application, the hashChangeListener prepends the '/' by calling ensureSlash() and destroys the hash for the outer application.

@taion
Copy link
Contributor

taion commented Nov 11, 2015

Huh. This seems a little odd to me.

By the same reasoning, if you were using two separate HTML5 routers, would you also want to ignore certain paths? I'm not sure it makes sense to add something unintuitive like this for supporting multiple routers trying to work off the same global location.

Or rather, this change in this place does not seem to match the use case you describe.

@cwrs
Copy link
Author

cwrs commented Nov 12, 2015

You're right, I would prefer to have one global history handler and I guess its not a common use case.
For the moment a user switches from one app to the other, we must prevent the hashChangeListener to call ensureSlash(). For us it would help a lot, if it would be possible to override or configure the ensureSlash() function within the createHashHistory module. I thought a ignore-Path function would be a more common use case.

@mjackson
Copy link
Member

So, you want a way for hash history to ignore all changes? Or just some changes? i.e. Is there ever going to be a case when you:

function ignorePath() {
  return true
}

?

@cwrs
Copy link
Author

cwrs commented Nov 12, 2015

I want the hashHistory to ignore all hashes that don't start with a '/'. For all other hashes I would return true.
Currently it automatically adds the '/' whenever a hash is not recognized as an absolute path.

@taion
Copy link
Contributor

taion commented Nov 12, 2015

This comes up in the context of social auth as well - some providers will use hash to provide auth information to the client application.

Would it make more sense to have createHashHistory just ignore all hashes that don't start with / globally? It seems like it'd fix both your use case and the social auth use case transparently, while not forcing users to do additional configuration.

I don't think we ever intentionally generate hashes that don't start with /, do we?

@cwrs
Copy link
Author

cwrs commented Nov 12, 2015

It would definitely match our use case, but it might destroy 404-handling for others I guess? But prepending a '/' on history-change might as well. I think just removing the ensureSlash-call on historyChange might work for both use cases as well?

@taion
Copy link
Contributor

taion commented Nov 12, 2015

I feel like if all of our paths start with /, it's just an error condition if you push a hash without it.

@taion
Copy link
Contributor

taion commented Nov 20, 2015

@cwrs What do you want to do here? Do you think it'd be too intrusive to always ignore paths that don't start with /?

@cwrs
Copy link
Author

cwrs commented Nov 20, 2015

I think its the right thing to do. I could update my pull-request next week. Sorry for answering so late, I'm on vacation right now.

@taion
Copy link
Contributor

taion commented Nov 20, 2015

No worries, just wanted to follow up.

@cwrs cwrs force-pushed the master branch 3 times, most recently from 4b18a43 to 8cf549e Compare November 23, 2015 15:23
@cwrs cwrs changed the title add option to ignore hashes when using hash-history Only handle hashes with a '/' when using createHashHistory Nov 23, 2015
@cwrs
Copy link
Author

cwrs commented Nov 23, 2015

I changed my pull-request, but I'm not sure, how to test the new behaviour...

@taion
Copy link
Contributor

taion commented Nov 23, 2015

Listen to the hash history with a spy, and count how many times that spy gets called.

@cwrs
Copy link
Author

cwrs commented Nov 24, 2015

I tried to write a test and integrated SinonJs for the spy. Testing the behavour directly in the browser works, but my test will not work. Am I calling the wrong methods?

@taion
Copy link
Contributor

taion commented Nov 24, 2015

A little confusingly, expect already has the capacity for creating spies. We'd prefer you use that for consistency.

@taion
Copy link
Contributor

taion commented Nov 26, 2015

@cwrs
Copy link
Author

cwrs commented Nov 26, 2015

Thx, that was unexpected :). Much easier than integrating sinon.

@cwrs
Copy link
Author

cwrs commented Nov 26, 2015

The tests work fine for me in Chrome... Do you have an idea what's wrong?

@ghost
Copy link

ghost commented Dec 28, 2015

I have a similar problem with oauth.

When auth0 is using the provided callback, it usually looks something like this: https://webiste.com/#access_token=abc

react-router interpret that as https://webiste.com/#/access_token=abc and complains that there is no route access_token.

Is there any way around that yet?

@taion
Copy link
Contributor

taion commented Jan 1, 2016

@cwrs The failures were in non-Chrome browsers on BrowserStack. At a glance, looks like not a false positive.

@cwrs
Copy link
Author

cwrs commented Jan 6, 2016

@taion thx, and sorry for answering so late. I will update my patch to the current version and see, if I can reproduce the errors on other browsers.

@cwrs cwrs force-pushed the master branch 2 times, most recently from 7315bb9 to ea7521a Compare January 11, 2016 08:47
@cwrs
Copy link
Author

cwrs commented Jan 11, 2016

My tests are now working, but I didn't change a thing since.

@@ -133,7 +133,7 @@ function createHashHistory(options={}) {

function listenBefore(listener) {
if (++listenerCount === 1)
stopHashChangeListener = startHashChangeListener(history)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Are these and other changes needed?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I mean, the ones of history -> this.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The changes are only needed for tests, to use a spy to check of transitionTo is called.

@taion
Copy link
Contributor

taion commented Jan 15, 2016

This is actually not enough to fix e.g. remix-run/react-router#2776.

The problem is that getCurrentLocation() for the initial location after the redirect is still going to fire with the "incorrect" location.

We might have to handle this some other way.

@ryanflorence
Copy link
Member

Seems like we should only handle changes that came from our API.

history.push(...)

// internally
handlingWithHistory = true

// then in the hashchange listener
if (handlingWithHistory)
  doStuff()

// then switch it back
handlingWithHistory = false

Then we'd be ignoring any hash changes not initiated by the library. I think paths shouldn't matter, but how the change was initiated that matters.

@taion
Copy link
Contributor

taion commented Jan 17, 2016

@ryanflorence

We need to deal with POP actions, though, as well as the initial hash location when the user first hits the page – neither of those are triggered directly by the history.

The PR actually gets us a good part of the way there – the problem is that in the case of getting redirected to e.g. my/page#auth_token=whatever by a third-party service, we still need to handle the case where the initial location is not "valid" in some sense.

@cwrs
Copy link
Author

cwrs commented Jan 19, 2016

would it be possible to merge my PR if I update it to master? Or should I try to handle the initial location issue?

@taion
Copy link
Contributor

taion commented Jan 19, 2016

@cwrs Do you have a good idea for handling the initial position issue? My initial thought is that it'd be better handled by the router than by history.

@taion
Copy link
Contributor

taion commented Jan 20, 2016

@cwrs Actually, does it help your use case at all if we just drop the ensureSlash behavior? Like, get rid of that function and drop everything that references it? You might still get some React Router warnings, but those shouldn't break you. We can then maybe find a better way to deal with this on the Router side if needed.

@cwrs
Copy link
Author

cwrs commented Jan 20, 2016

I'm currently testing a history-version without the ensureSlash() in our app. I was about to ask, if we can't drop the ensureSlash myself. All history-tests run well. So I'm totally fine with that.

@cwrs cwrs force-pushed the master branch 5 times, most recently from 432c402 to 680f7e8 Compare January 21, 2016 16:57
- dropped ensureSlash from createHashHistory

Change-Id: Ibed14c8acde9c1ea022508c29be8b04df175d466
@cwrs
Copy link
Author

cwrs commented Feb 23, 2016

The version without emsureSlash is running in production for us now for about 4 weeks without problems. I rebased my patch to the current master.

@taion
Copy link
Contributor

taion commented Feb 23, 2016

ping @mjackson

@cwrs
Copy link
Author

cwrs commented Jul 21, 2016

I guess with the new hashType option (6be82b0) this pull-request is now obsolete.

@cwrs cwrs closed this Jul 21, 2016
@taion
Copy link
Contributor

taion commented Jul 21, 2016

Thanks for putting up this PR.

@cwrs
Copy link
Author

cwrs commented Jul 21, 2016

Thx for all your great work!

@lock lock bot locked as resolved and limited conversation to collaborators Jan 18, 2019
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants