getInitialProps

Recommended: getStaticProps or getServerSideProps instead of getInitialProps. These data fetching methods allow you to have a granular choice between static generation and server-side rendering.

getInitialProps enables server-side rendering in a page and allows you to do initial data population, it means sending the page with the data already populated from the server. This is especially useful for SEO.

getInitialProps will disable Automatic Static Optimization.

getInitialProps is an async function that can be added to any page as a static method. Take a look at the following example:

  1. function Page({ stars }) {
  2. return <div>Next stars: {stars}</div>
  3. }
  4. Page.getInitialProps = async (ctx) => {
  5. const res = await fetch('https://api.github.com/repos/vercel/next.js')
  6. const json = await res.json()
  7. return { stars: json.stargazers_count }
  8. }
  9. export default Page

Or using a class component:

  1. import React from 'react'
  2. class Page extends React.Component {
  3. static async getInitialProps(ctx) {
  4. const res = await fetch('https://api.github.com/repos/vercel/next.js')
  5. const json = await res.json()
  6. return { stars: json.stargazers_count }
  7. }
  8. render() {
  9. return <div>Next stars: {this.props.stars}</div>
  10. }
  11. }
  12. export default Page

getInitialProps is used to asynchronously fetch some data, which then populates props.

Data returned from getInitialProps is serialized when server rendering, similar to what JSON.stringify does. Make sure the returned object from getInitialProps is a plain Object and not using Date, Map or Set.

For the initial page load, getInitialProps will run on the server only. getInitialProps will then run on the client when navigating to a different route via the next/link component or by using next/router. However, if getInitialProps is used in a custom _app.js, and the page being navigated to implements getServerSideProps, then getInitialProps will run on the server.

Context Object

getInitialProps receives a single argument called context, it’s an object with the following properties:

  • pathname - Current route. That is the path of the page in /pages
  • query - Query string section of URL parsed as an object
  • asPath - String of the actual path (including the query) shown in the browser
  • req - HTTP request object (server only)
  • res - HTTP response object (server only)
  • err - Error object if any error is encountered during the rendering

Caveats

  • getInitialProps can not be used in children components, only in the default export of every page
  • If you are using server-side only modules inside getInitialProps, make sure to import them properly, otherwise it’ll slow down your app

Note that irrespective of rendering type, any props will be passed to the page component and can be viewed on the client-side in the initial HTML. This is to allow the page to be hydrated correctly. Make sure that you don’t pass any sensitive information that shouldn’t be available on the client in props.

TypeScript

If you’re using TypeScript, you can use the NextPage type for function components:

  1. import { NextPage } from 'next'
  2. interface Props {
  3. userAgent?: string;
  4. }
  5. const Page: NextPage<Props> = ({ userAgent }) => (
  6. <main>Your user agent: {userAgent}</main>
  7. )
  8. Page.getInitialProps = async ({ req }) => {
  9. const userAgent = req ? req.headers['user-agent'] : navigator.userAgent
  10. return { userAgent }
  11. }
  12. export default Page

And for React.Component, you can use NextPageContext:

  1. import React from 'react'
  2. import { NextPageContext } from 'next'
  3. interface Props {
  4. userAgent?: string;
  5. }
  6. export default class Page extends React.Component<Props> {
  7. static async getInitialProps({ req }: NextPageContext) {
  8. const userAgent = req ? req.headers['user-agent'] : navigator.userAgent
  9. return { userAgent }
  10. }
  11. render() {
  12. const { userAgent } = this.props
  13. return <main>Your user agent: {userAgent}</main>
  14. }
  15. }

For more information on what to do next, we recommend the following sections:

Data FetchingLearn more about data fetching in Next.js.