{"pageProps":{"routes":[{"title":"Documentation","heading":true,"routes":[{"title":"Getting Started","path":"/docs/getting-started.md"},{"title":"Basic Features","open":true,"routes":[{"title":"Pages","path":"/docs/basic-features/pages.md"},{"title":"Data fetching","path":"/docs/basic-features/data-fetching.md"},{"title":"Built-in CSS Support","path":"/docs/basic-features/built-in-css-support.md"},{"title":"Static File Serving","path":"/docs/basic-features/static-file-serving.md"},{"title":"TypeScript","path":"/docs/basic-features/typescript.md"},{"title":"Environment Variables","path":"/docs/basic-features/environment-variables.md"}]},{"title":"Routing","routes":[{"title":"Introduction","path":"/docs/routing/introduction.md"},{"title":"Dynamic Routes","path":"/docs/routing/dynamic-routes.md"},{"title":"Imperatively","path":"/docs/routing/imperatively.md"},{"title":"Shallow Routing","path":"/docs/routing/shallow-routing.md"}]},{"title":"API Routes","routes":[{"title":"Introduction","path":"/docs/api-routes/introduction.md"},{"title":"Dynamic API Routes","path":"/docs/api-routes/dynamic-api-routes.md"},{"title":"API Middlewares","path":"/docs/api-routes/api-middlewares.md"},{"title":"Response Helpers","path":"/docs/api-routes/response-helpers.md"}]},{"title":"Deployment","path":"/docs/deployment.md"},{"title":"Advanced Features","routes":[{"title":"Preview Mode","path":"/docs/advanced-features/preview-mode.md"},{"title":"Dynamic Import","path":"/docs/advanced-features/dynamic-import.md"},{"title":"Automatic Static Optimization","path":"/docs/advanced-features/automatic-static-optimization.md"},{"title":"Static HTML Export","path":"/docs/advanced-features/static-html-export.md"},{"title":"Absolute Imports and Module Path Aliases","path":"/docs/advanced-features/module-path-aliases.md"},{"title":"AMP Support","routes":[{"title":"Introduction","path":"/docs/advanced-features/amp-support/introduction.md"},{"title":"Adding AMP Components","path":"/docs/advanced-features/amp-support/adding-amp-components.md"},{"title":"AMP Validation","path":"/docs/advanced-features/amp-support/amp-validation.md"},{"title":"AMP in Static HTML export","path":"/docs/advanced-features/amp-support/amp-in-static-html-export.md"},{"title":"TypeScript","path":"/docs/advanced-features/amp-support/typescript.md"}]},{"title":"Customizing Babel Config","path":"/docs/advanced-features/customizing-babel-config.md"},{"title":"Customizing PostCSS Config","path":"/docs/advanced-features/customizing-postcss-config.md"},{"title":"Custom Server","path":"/docs/advanced-features/custom-server.md"},{"title":"Custom `App`","path":"/docs/advanced-features/custom-app.md"},{"title":"Custom `Document`","path":"/docs/advanced-features/custom-document.md"},{"title":"Custom Error Page","path":"/docs/advanced-features/custom-error-page.md"},{"title":"`src` Directory","path":"/docs/advanced-features/src-directory.md"},{"title":"Multi Zones","path":"/docs/advanced-features/multi-zones.md"},{"title":"Measuring performance","path":"/docs/advanced-features/measuring-performance.md"}]},{"title":"Upgrade Guide","path":"/docs/upgrading.md"},{"title":"FAQ","path":"/docs/faq.md"}]},{"title":"API Reference","heading":true,"routes":[{"title":"CLI","path":"/docs/api-reference/cli.md"},{"title":"next/router","path":"/docs/api-reference/next/router.md"},{"title":"next/link","path":"/docs/api-reference/next/link.md"},{"title":"next/head","path":"/docs/api-reference/next/head.md"},{"title":"next/amp","path":"/docs/api-reference/next/amp.md"},{"title":"Data Fetching","routes":[{"title":"getInitialProps","path":"/docs/api-reference/data-fetching/getInitialProps.md"}]},{"title":"next.config.js","routes":[{"title":"Introduction","path":"/docs/api-reference/next.config.js/introduction.md"},{"title":"Environment Variables","path":"/docs/api-reference/next.config.js/environment-variables.md"},{"title":"Custom Page Extensions","path":"/docs/api-reference/next.config.js/custom-page-extensions.md"},{"title":"CDN Support with Asset Prefix","path":"/docs/api-reference/next.config.js/cdn-support-with-asset-prefix.md"},{"title":"Build Target","path":"/docs/api-reference/next.config.js/build-target.md"},{"title":"Custom Webpack Config","path":"/docs/api-reference/next.config.js/custom-webpack-config.md"},{"title":"Compression","path":"/docs/api-reference/next.config.js/compression.md"},{"title":"Static Optimization Indicator","path":"/docs/api-reference/next.config.js/static-optimization-indicator.md"},{"title":"Runtime Configuration","path":"/docs/api-reference/next.config.js/runtime-configuration.md"},{"title":"Disabling x-powered-by","path":"/docs/api-reference/next.config.js/disabling-x-powered-by.md"},{"title":"Disabling ETag Generation","path":"/docs/api-reference/next.config.js/disabling-etag-generation.md"},{"title":"Setting a custom build directory","path":"/docs/api-reference/next.config.js/setting-a-custom-build-directory.md"},{"title":"Configuring the Build ID","path":"/docs/api-reference/next.config.js/configuring-the-build-id.md"},{"title":"Configuring onDemandEntries","path":"/docs/api-reference/next.config.js/configuring-onDemandEntries.md"},{"title":"Ignoring TypeScript Errors","path":"/docs/api-reference/next.config.js/ignoring-typescript-errors.md"},{"title":"exportPathMap","path":"/docs/api-reference/next.config.js/exportPathMap.md"}]}]}],"data":{"description":"Enable Server-Side Rendering in a page and do initial data population with `getInitialProps`."},"route":{"title":"getInitialProps","path":"/docs/api-reference/data-fetching/getInitialProps.md"},"html":"
\n\nRecommended:
\ngetStaticProps
orgetServerSideProps
If you're using Next.js 9.3 or newer, we recommend that you use
\ngetStaticProps
orgetServerSideProps
instead ofgetInitialProps
.These new data fetching methods allow you to have a granular choice between static generation and server-side rendering.\nLearn more on the documentation for Pages and Data fetching:
\n
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.
\n\n\n
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:
function Page({ stars }) {\n return <div>Next stars: {stars}</div>\n}\n\nPage.getInitialProps = async ctx => {\n const res = await fetch('https://api.github.com/repos/zeit/next.js')\n const json = await res.json()\n return { stars: json.stargazers_count }\n}\n\nexport default Page\n
\nOr using a class component:
\nimport React from 'react'\n\nclass Page extends React.Component {\n static async getInitialProps(ctx) {\n const res = await fetch('https://api.github.com/repos/zeit/next.js')\n const json = await res.json()\n return { stars: json.stargazers_count }\n }\n\n render() {\n return <div>Next stars: {this.props.stars}</div>\n }\n}\n\nexport default Page\n
\ngetInitialProps
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
.
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 objectasPath
- String
of the actual path (including the query) shown in the browserreq
- HTTP request object (server only)res
- HTTP response object (server only)err
- Error object if any error is encountered during the renderinggetInitialProps
can not be used in children components, only in the default export of every pagegetInitialProps
, make sure to import them properly, otherwise it'll slow down your appIf you're using TypeScript, you can use the NextPage
type for function components:
import { NextPage } from 'next'\n\ninterface Props {\n userAgent?: string;\n}\n\nconst Page: NextPage<Props> = ({ userAgent }) => (\n <main>Your user agent: {userAgent}</main>\n)\n\nPage.getInitialProps = async ({ req }) => {\n const userAgent = req ? req.headers['user-agent'] : navigator.userAgent\n return { userAgent }\n}\n\nexport default Page\n
\nAnd for React.Component
, you can use NextPageContext
:
import React from 'react'\nimport { NextPageContext } from 'next'\n\ninterface Props {\n userAgent?: string;\n}\n\nexport default class Page extends React.Component<Props> {\n static async getInitialProps({ req }: NextPageContext) {\n const userAgent = req ? req.headers['user-agent'] : navigator.userAgent\n return { userAgent }\n }\n\n render() {\n const { userAgent } = this.props\n return <main>Your user agent: {userAgent}</main>\n }\n}\n
\nFor more information on what to do next, we recommend the following sections:
\n\n\n"},"__N_SSG":true}