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

accessing parsed url in getInitialProps #62

Closed
bmcmahen opened this issue Oct 25, 2016 · 5 comments
Closed

accessing parsed url in getInitialProps #62

bmcmahen opened this issue Oct 25, 2016 · 5 comments

Comments

@bmcmahen
Copy link

As far as I can tell, the static getInitialProps doesn't provide a uniform way to access url params on the client and server. On the server, you can access req and parse the url yourself. On the client, we don't seem to have access to the route information within the getInitialProps function. The uses for this are fairly obvious, i think, such as loading a particular user profile based on an :id param, and so on. I'm also wondering if auth could somehow be implemented here too -- thought I'm less clear if that's the best API.

@impronunciable
Copy link
Contributor

@bmcmahen Actually there is and it's featured here https://github.com/zeit/nextgram/blob/master/pages/photo.js#L5

there is a demo for that app at https://nextgram.now.sh

@bmcmahen
Copy link
Author

But that's accessible only in the render function (or stateless functional components), and not the getInitialProps static function.

@danoc
Copy link

danoc commented Oct 31, 2016

@bmcmahen – Have you found a way to do this?

Running into the same problem now.

When rendering client side, getInitialProps receives an object that looks like this:

{
  err: undefined,
  xhr: XMLHttpRequest
}

When rendering server side, it receives an object like this:

{
  req: IncomingMessage,
  res: ServerResponse,
}

I use getInitialProps make an API call that requires parsing the URL and grabbing a value from the query parameters. This can be done in the server side approach by accessing the url property of IncomingMessage, but AFAIK, there is no way to do this client side.

@nkzawa nkzawa added the bug label Oct 31, 2016
@nkzawa
Copy link
Contributor

nkzawa commented Oct 31, 2016

This should be a kind of bugs to be fixed. One way is to execute history.pushState before calling getIntialProps, so that you can just access window.location.

I think it's great since it's similar to what happens on popstate. First url is changed, second a component is fetched on both pushstate and popstate.

cc @rauchg

@rauchg
Copy link
Member

rauchg commented Nov 3, 2016

I accidentally duplicated this here: #188

@lock lock bot locked as resolved and limited conversation to collaborators May 12, 2018
ijjk pushed a commit to ijjk/next.js that referenced this issue Apr 13, 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

No branches or pull requests

5 participants