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

How to handle hashes in URL? #779

Closed
astanciu opened this issue Jan 15, 2017 · 3 comments
Closed

How to handle hashes in URL? #779

astanciu opened this issue Jan 15, 2017 · 3 comments

Comments

@astanciu
Copy link

A lot of OAuth based callback urls will take the form of:
http://site.com/login#state=123&code=123

Is there any way to access that hash? Would be great if props.url had a .hash property (alongside .query)

Or is there an easier way to get this?

@timneutkens
Copy link
Member

@astanciu there is one major problem here. Fragments (#) are unfortunately never sent to the server. So if you'd do a refresh you would lose the state. There is currently no property available to get the hash. Though you could do something like this:

  static getInitialProps ({ req }) {
    let hash = ''
    if(!req)
      hash = window.location.hash
    }

    return { hash }
  }

Then again, this will not get the hash when server side rendering.

@arunoda can you verify this? getInitialProps is only called once if I remember correctly, either server or client. So we can't implement this correctly.

@frol
Copy link
Contributor

frol commented Jan 15, 2017

Browsers, by design, don't include URL hash in HTTP requests, so it is simply impossible to handle on the server side. I would just use componentDidMount instead of getInitialProps.

@timneutkens
Copy link
Member

@frol you're right, using componentDidMount is way better. Since it is called on frontend render, thanks ❤️

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

No branches or pull requests

3 participants