Skip to content

Commit

Permalink
Improve automatic prerendering warning (vercel#7759)
Browse files Browse the repository at this point in the history
  • Loading branch information
Timer authored Jul 5, 2019
1 parent 80b46c1 commit 6fa17b3
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 1 deletion.
35 changes: 35 additions & 0 deletions errors/opt-out-automatic-prerendering.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
# Opt-out of Automatic Prerendering

#### Why This Warning Occurred

You are using `getInitialProps` in your [Custom `<App>`](https://github.com/zeit/next.js#custom-app).

This causes **all pages** to be executed on the server -- disabling [Automatic Prerendering](https://github.com/zeit/next.js#automatic-prerendering).

#### Possible Ways to Fix It

Be sure you meant to use `getInitialProps` in `pages/_app`!
There are some valid use cases for this, but it is often better to handle `getInitialProps` on a _per-page_ basis.

If you copied the [Custom `<App>`](https://github.com/zeit/next.js#custom-app) example, you may be able to remove your `getInitialProps`.

The following `getInitialProps` does nothing and may be removed:

```js
class MyApp extends App {
// Remove me, I do nothing!
static async getInitialProps({ Component, ctx }) {
let pageProps = {}

if (Component.getInitialProps) {
pageProps = await Component.getInitialProps(ctx)
}

return { pageProps }
}

render() {
// ...
}
}
```
8 changes: 7 additions & 1 deletion packages/next/build/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -329,7 +329,13 @@ export default async function build(dir: string, conf = null): Promise<void> {

if (customAppGetInitialProps) {
console.warn(
'Opting out of automatic exporting due to custom `getInitialProps` in `pages/_app`\n'
chalk.bold.yellow(`Warning: `) +
chalk.yellow(
`You have opted-out of Automatic Prerendering due to \`getInitialProps\` in \`pages/_app\`.`
)
)
console.warn(
'Read more: https://err.sh/next.js/opt-out-automatic-prerendering\n'
)
}
}
Expand Down

0 comments on commit 6fa17b3

Please sign in to comment.