Next.js: Prevent getInitialProps client-side

Created on 26 Dec 2016  路  2Comments  路  Source: vercel/next.js

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?

Most helpful comment

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 {} 
  }
}

All 2 comments

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 {} 
  }
}

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.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

kenji4569 picture kenji4569  路  3Comments

irrigator picture irrigator  路  3Comments

formula349 picture formula349  路  3Comments

jesselee34 picture jesselee34  路  3Comments

flybayer picture flybayer  路  3Comments