@@ -4,6 +4,14 @@ description: Enable Server-Side Rendering in a page and do initial data populati
...
@@ -4,6 +4,14 @@ description: Enable Server-Side Rendering in a page and do initial data populati
# getInitialProps
# getInitialProps
## Recommended: Use `getStaticProps` or `getServerSideProps` instead
If you're using Next.js 9.3 or newer, you should use `getStaticProps` or `getServerSideProps` instead of `getInitialProps`.
Learn more on the [Pages documentation](/docs/basic-features/pages.md) and the [Data fetching documentation](/docs/basic-features/data-fetching.md):
## `getInitialProps` (for older versions of Next.js)
<details>
<details>
<summary><b>Examples</b></summary>
<summary><b>Examples</b></summary>
<ul>
<ul>
...
@@ -76,6 +84,52 @@ For the initial page load, `getInitialProps` will execute on the server only. `g
...
@@ -76,6 +84,52 @@ For the initial page load, `getInitialProps` will execute on the server only. `g
-`getInitialProps` can **not** be used in children components, only in the default export of every page
-`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](https://arunoda.me/blog/ssr-and-server-only-modules), otherwise it'll slow down your app
- If you are using server-side only modules inside `getInitialProps`, make sure to [import them properly](https://arunoda.me/blog/ssr-and-server-only-modules), otherwise it'll slow down your app
## TypeScript
If you're using TypeScript, you can use the `NextPage` type for functional components:
@@ -82,6 +82,18 @@ You should use `getStaticProps` if:
...
@@ -82,6 +82,18 @@ You should use `getStaticProps` if:
- The data can be publicly cached (not user-specific).
- The data can be publicly cached (not user-specific).
- The page must be pre-rendered (for SEO) and be very fast — `getStaticProps` generates HTML and JSON files, both of which can be cached by a CDN for performance.
- The page must be pre-rendered (for SEO) and be very fast — `getStaticProps` generates HTML and JSON files, both of which can be cached by a CDN for performance.
### TypeScript: Use `GetStaticProps`
For TypeScript, you can use the `GetStaticProps` type from `next`:
@@ -344,6 +368,18 @@ You should use `getServerSideProps` only if you need to pre-render a page whose
...
@@ -344,6 +368,18 @@ You should use `getServerSideProps` only if you need to pre-render a page whose
If you don’t need to pre-render the data, then you should consider fetching data on the client side. [Click here to learn more](#fetching-data-on-the-client-side).
If you don’t need to pre-render the data, then you should consider fetching data on the client side. [Click here to learn more](#fetching-data-on-the-client-side).
### TypeScript: Use `GetServerSideProps`
For TypeScript, you can use the `GetServerSideProps` type from `next`:
@@ -47,52 +47,28 @@ By default, Next.js reports TypeScript errors during development for pages you a
...
@@ -47,52 +47,28 @@ By default, Next.js reports TypeScript errors during development for pages you a
If you want to silence the error reports, refer to the documentation for [Ignoring TypeScript errors](/docs/api-reference/next.config.js/ignoring-typescript-errors.md).
If you want to silence the error reports, refer to the documentation for [Ignoring TypeScript errors](/docs/api-reference/next.config.js/ignoring-typescript-errors.md).
## Pages
## Static Generation and Server-side Rendering
For function components the `NextPage` type is exported, here's how to use it:
For `getStaticProps`, `getStaticPaths`, and `getServerSideProps`, you can use the `GetStaticProps`, `GetStaticPaths`, and `GetServerSideProps` types respectively: