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

Prevent getInitialProps client-side #524

Closed
sebasgarcep opened this issue Dec 26, 2016 · 2 comments
Closed

Prevent getInitialProps client-side #524

sebasgarcep opened this issue Dec 26, 2016 · 2 comments

Comments

@sebasgarcep
Copy link

Currently getInitialProps runs once on the server and once on the client. Is there a way to disable it from running on the client or preserving the props it got from the server render on certain pages?

@nkzawa
Copy link
Contributor

nkzawa commented Dec 26, 2016

To make it clear, getInitialProps runs on server if it's server rendering, and does on client if page transition occurs by using Link component or props.url.push().

If you don't want to provide props on client for example, you can write like the following.

const isServer = typeof window === 'undefined'
static getInitialProps () {
  if (isServer) {
    return { value: 'hi' }
  } else {
    return {} 
  }
}

@nkzawa nkzawa closed this as completed Dec 26, 2016
@sebasgarcep
Copy link
Author

sebasgarcep commented Dec 26, 2016

For some reason getInitialProps is running on the client as soon as it mounts. Maybe it's because I'm using next's programmatic API. I'll be looking into it to see if it's a problem on my end.

@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

2 participants