rsnext/docs/advanced-features/custom-app.md

71 lines
3.2 KiB
Markdown
Raw Normal View History

---
description: Control page initialization and add a layout that persists for all pages by overriding the default App component used by Next.js.
---
New docs (#9301) * Added the docs from Notion * Updated the links from notion to relative links * Added a routes manifest to the docs * Removed the <br> after examples toggle * Use the name of the section instead of Introduction * Fixed spelling errors * Optimize the content for Algolia * Add a paragraph for `pageProps` * Add welcome section * Transpile -> Compile * Update getting-started.md * Update getting-started.md * Update getting-started.md * Update getting-started.md * Update getting-started.md * Update getting-started.md * Update getting-started.md * Update getting-started.md * Update getting-started.md * Update getting-started.md * Test extra room between * Update getting-started.md * Update getting-started.md * Update getting-started.md * Update getting-started.md * Update getting-started.md * Update manifest.json * Update getting-started.md * Update getting-started.md * Add concepts section * Update pages.md * Update pages.md * Add data fetching section * Update pages.md * See how a card looks like * Undo card changes * Added related section to getting-started * Fixed wrong markdown syntax in the withRouter page * Moved the server-side-and-client-side section * Updated next-cli reference * updated getInitialProps section * Minor fixes * Added more Related sections * Add html to the related section in getting-started * Use small for the card * Use cards for all related sections * Added src directory docs * Added src directory to the manifest * Add note about API routes in next export * Add initial data fetching docs (private until new methods are released) * Fix typos * Improve wording * Update getting-started.md * Update getting-started.md * Move advanced concepts to advanced section * Hide server-side vs client-side * Move AMP support * Move typescript into one page * Add routing concepts page * Remove introduction page * Update section on different route types * Update routing.md * Update routing.md * Update routing.md * Update routing.md * Combine router injection pages * Update pages.md * Update routing.md * Update using-link.md * Update using-link.md * Update typescript.md * Move the API Routes typescript to basic features * Added links to the typescript section * Updated links to useRouter and withRouter * Add singleLevel prop to manifest * Added single page for router docs * Updated description * Updated the routes in the manifest * Add data fetching section * Update data-fetching.md * Update data-fetching.md * Update dynamic-routes.md * Update manifest.json * Only use the single router API page * Moved the concepts pages * Updated links * Removed extra space * Updated title for Router API * Added a description with frontmatter * Add open prop to the manifest * Added datafetching section to API Reference * Updated links to the getInitialProps reference * Moved some sections to API * Added next/head to API reference * Added next/link to the API Reference * Removed the populating-head section * Updated links to the new next/link API * Added link from dynamic-routes to next/link docs * use a paragraph * Added next/router API * Added next/amp * Updated the docs for next/amp * Moved the AMP support folder * Updated title * Content updates * Added more links to the data fetching section * Added links from the API to introductions * changing the router API * Updates to the router API * Updated the routing section * life improvements * Added shallow routing section * Small fix * Removed old routing sections * Updated link to shallow routing * Removed unrequired page * Removed /pages * Update data-fetching.md * Add initial deployments section * Update manifest.json * Update introduction.md * Update deployment doc * Add static export section updates * link ssg/ssr * Update deployment.md * Add syntax highlighting Co-authored-by: Tim Neutkens <tim@timneutkens.nl>
2019-12-23 16:07:38 +01:00
# Custom `App`
Next.js uses the `App` component to initialize pages. You can override it and control the page initialization. Which allows you to do amazing things like:
- Persisting layout between page changes
- Keeping state when navigating pages
- Custom error handling using `componentDidCatch`
- Inject additional data into pages
- [Add global CSS](/docs/basic-features/built-in-css-support.md#adding-a-global-stylesheet)
New docs (#9301) * Added the docs from Notion * Updated the links from notion to relative links * Added a routes manifest to the docs * Removed the <br> after examples toggle * Use the name of the section instead of Introduction * Fixed spelling errors * Optimize the content for Algolia * Add a paragraph for `pageProps` * Add welcome section * Transpile -> Compile * Update getting-started.md * Update getting-started.md * Update getting-started.md * Update getting-started.md * Update getting-started.md * Update getting-started.md * Update getting-started.md * Update getting-started.md * Update getting-started.md * Update getting-started.md * Test extra room between * Update getting-started.md * Update getting-started.md * Update getting-started.md * Update getting-started.md * Update getting-started.md * Update manifest.json * Update getting-started.md * Update getting-started.md * Add concepts section * Update pages.md * Update pages.md * Add data fetching section * Update pages.md * See how a card looks like * Undo card changes * Added related section to getting-started * Fixed wrong markdown syntax in the withRouter page * Moved the server-side-and-client-side section * Updated next-cli reference * updated getInitialProps section * Minor fixes * Added more Related sections * Add html to the related section in getting-started * Use small for the card * Use cards for all related sections * Added src directory docs * Added src directory to the manifest * Add note about API routes in next export * Add initial data fetching docs (private until new methods are released) * Fix typos * Improve wording * Update getting-started.md * Update getting-started.md * Move advanced concepts to advanced section * Hide server-side vs client-side * Move AMP support * Move typescript into one page * Add routing concepts page * Remove introduction page * Update section on different route types * Update routing.md * Update routing.md * Update routing.md * Update routing.md * Combine router injection pages * Update pages.md * Update routing.md * Update using-link.md * Update using-link.md * Update typescript.md * Move the API Routes typescript to basic features * Added links to the typescript section * Updated links to useRouter and withRouter * Add singleLevel prop to manifest * Added single page for router docs * Updated description * Updated the routes in the manifest * Add data fetching section * Update data-fetching.md * Update data-fetching.md * Update dynamic-routes.md * Update manifest.json * Only use the single router API page * Moved the concepts pages * Updated links * Removed extra space * Updated title for Router API * Added a description with frontmatter * Add open prop to the manifest * Added datafetching section to API Reference * Updated links to the getInitialProps reference * Moved some sections to API * Added next/head to API reference * Added next/link to the API Reference * Removed the populating-head section * Updated links to the new next/link API * Added link from dynamic-routes to next/link docs * use a paragraph * Added next/router API * Added next/amp * Updated the docs for next/amp * Moved the AMP support folder * Updated title * Content updates * Added more links to the data fetching section * Added links from the API to introductions * changing the router API * Updates to the router API * Updated the routing section * life improvements * Added shallow routing section * Small fix * Removed old routing sections * Updated link to shallow routing * Removed unrequired page * Removed /pages * Update data-fetching.md * Add initial deployments section * Update manifest.json * Update introduction.md * Update deployment doc * Add static export section updates * link ssg/ssr * Update deployment.md * Add syntax highlighting Co-authored-by: Tim Neutkens <tim@timneutkens.nl>
2019-12-23 16:07:38 +01:00
To override the default `App`, create the file `./pages/_app.js` as shown below:
```jsx
// import App from 'next/app'
New docs (#9301) * Added the docs from Notion * Updated the links from notion to relative links * Added a routes manifest to the docs * Removed the <br> after examples toggle * Use the name of the section instead of Introduction * Fixed spelling errors * Optimize the content for Algolia * Add a paragraph for `pageProps` * Add welcome section * Transpile -> Compile * Update getting-started.md * Update getting-started.md * Update getting-started.md * Update getting-started.md * Update getting-started.md * Update getting-started.md * Update getting-started.md * Update getting-started.md * Update getting-started.md * Update getting-started.md * Test extra room between * Update getting-started.md * Update getting-started.md * Update getting-started.md * Update getting-started.md * Update getting-started.md * Update manifest.json * Update getting-started.md * Update getting-started.md * Add concepts section * Update pages.md * Update pages.md * Add data fetching section * Update pages.md * See how a card looks like * Undo card changes * Added related section to getting-started * Fixed wrong markdown syntax in the withRouter page * Moved the server-side-and-client-side section * Updated next-cli reference * updated getInitialProps section * Minor fixes * Added more Related sections * Add html to the related section in getting-started * Use small for the card * Use cards for all related sections * Added src directory docs * Added src directory to the manifest * Add note about API routes in next export * Add initial data fetching docs (private until new methods are released) * Fix typos * Improve wording * Update getting-started.md * Update getting-started.md * Move advanced concepts to advanced section * Hide server-side vs client-side * Move AMP support * Move typescript into one page * Add routing concepts page * Remove introduction page * Update section on different route types * Update routing.md * Update routing.md * Update routing.md * Update routing.md * Combine router injection pages * Update pages.md * Update routing.md * Update using-link.md * Update using-link.md * Update typescript.md * Move the API Routes typescript to basic features * Added links to the typescript section * Updated links to useRouter and withRouter * Add singleLevel prop to manifest * Added single page for router docs * Updated description * Updated the routes in the manifest * Add data fetching section * Update data-fetching.md * Update data-fetching.md * Update dynamic-routes.md * Update manifest.json * Only use the single router API page * Moved the concepts pages * Updated links * Removed extra space * Updated title for Router API * Added a description with frontmatter * Add open prop to the manifest * Added datafetching section to API Reference * Updated links to the getInitialProps reference * Moved some sections to API * Added next/head to API reference * Added next/link to the API Reference * Removed the populating-head section * Updated links to the new next/link API * Added link from dynamic-routes to next/link docs * use a paragraph * Added next/router API * Added next/amp * Updated the docs for next/amp * Moved the AMP support folder * Updated title * Content updates * Added more links to the data fetching section * Added links from the API to introductions * changing the router API * Updates to the router API * Updated the routing section * life improvements * Added shallow routing section * Small fix * Removed old routing sections * Updated link to shallow routing * Removed unrequired page * Removed /pages * Update data-fetching.md * Add initial deployments section * Update manifest.json * Update introduction.md * Update deployment doc * Add static export section updates * link ssg/ssr * Update deployment.md * Add syntax highlighting Co-authored-by: Tim Neutkens <tim@timneutkens.nl>
2019-12-23 16:07:38 +01:00
function MyApp({ Component, pageProps }) {
return <Component {...pageProps} />
}
// Only uncomment this method if you have blocking data requirements for
// every single page in your application. This disables the ability to
// perform automatic static optimization, causing every page in your app to
// be server-side rendered.
//
// MyApp.getInitialProps = async (appContext) => {
// // calls page's `getInitialProps` and fills `appProps.pageProps`
// const appProps = await App.getInitialProps(appContext);
//
// return { ...appProps }
// }
export default MyApp
```
The `Component` prop is the active `page`, so whenever you navigate between routes, `Component` will change to the new `page`. Therefore, any props you send to `Component` will be received by the `page`.
`pageProps` is an object with the initial props that were preloaded for your page by one of our [data fetching methods](/docs/basic-features/data-fetching.md), otherwise it's an empty object.
New docs (#9301) * Added the docs from Notion * Updated the links from notion to relative links * Added a routes manifest to the docs * Removed the <br> after examples toggle * Use the name of the section instead of Introduction * Fixed spelling errors * Optimize the content for Algolia * Add a paragraph for `pageProps` * Add welcome section * Transpile -> Compile * Update getting-started.md * Update getting-started.md * Update getting-started.md * Update getting-started.md * Update getting-started.md * Update getting-started.md * Update getting-started.md * Update getting-started.md * Update getting-started.md * Update getting-started.md * Test extra room between * Update getting-started.md * Update getting-started.md * Update getting-started.md * Update getting-started.md * Update getting-started.md * Update manifest.json * Update getting-started.md * Update getting-started.md * Add concepts section * Update pages.md * Update pages.md * Add data fetching section * Update pages.md * See how a card looks like * Undo card changes * Added related section to getting-started * Fixed wrong markdown syntax in the withRouter page * Moved the server-side-and-client-side section * Updated next-cli reference * updated getInitialProps section * Minor fixes * Added more Related sections * Add html to the related section in getting-started * Use small for the card * Use cards for all related sections * Added src directory docs * Added src directory to the manifest * Add note about API routes in next export * Add initial data fetching docs (private until new methods are released) * Fix typos * Improve wording * Update getting-started.md * Update getting-started.md * Move advanced concepts to advanced section * Hide server-side vs client-side * Move AMP support * Move typescript into one page * Add routing concepts page * Remove introduction page * Update section on different route types * Update routing.md * Update routing.md * Update routing.md * Update routing.md * Combine router injection pages * Update pages.md * Update routing.md * Update using-link.md * Update using-link.md * Update typescript.md * Move the API Routes typescript to basic features * Added links to the typescript section * Updated links to useRouter and withRouter * Add singleLevel prop to manifest * Added single page for router docs * Updated description * Updated the routes in the manifest * Add data fetching section * Update data-fetching.md * Update data-fetching.md * Update dynamic-routes.md * Update manifest.json * Only use the single router API page * Moved the concepts pages * Updated links * Removed extra space * Updated title for Router API * Added a description with frontmatter * Add open prop to the manifest * Added datafetching section to API Reference * Updated links to the getInitialProps reference * Moved some sections to API * Added next/head to API reference * Added next/link to the API Reference * Removed the populating-head section * Updated links to the new next/link API * Added link from dynamic-routes to next/link docs * use a paragraph * Added next/router API * Added next/amp * Updated the docs for next/amp * Moved the AMP support folder * Updated title * Content updates * Added more links to the data fetching section * Added links from the API to introductions * changing the router API * Updates to the router API * Updated the routing section * life improvements * Added shallow routing section * Small fix * Removed old routing sections * Updated link to shallow routing * Removed unrequired page * Removed /pages * Update data-fetching.md * Add initial deployments section * Update manifest.json * Update introduction.md * Update deployment doc * Add static export section updates * link ssg/ssr * Update deployment.md * Add syntax highlighting Co-authored-by: Tim Neutkens <tim@timneutkens.nl>
2019-12-23 16:07:38 +01:00
### Caveats
- If your app is running and you just added a custom `App`, you'll need to restart the development server. Only required if `pages/_app.js` didn't exist before.
- Adding a custom `getInitialProps` in your `App` will disable [Automatic Static Optimization](/docs/advanced-features/automatic-static-optimization.md) in pages without [Static Generation](/docs/basic-features/data-fetching.md#getstaticprops-static-generation).
- When you add `getInitialProps` in your custom app, you must `import App from "next/app"`, call `App.getInitialProps(appContext)` inside `getInitialProps` and merge the returned object into the return value.
- `App` currently does not support Next.js [Data Fetching methods](/docs/basic-features/data-fetching.md) like [`getStaticProps`](/docs/basic-features/data-fetching.md#getstaticprops-static-generation) or [`getServerSideProps`](/docs/basic-features/data-fetching.md#getserversideprops-server-side-rendering).
New docs (#9301) * Added the docs from Notion * Updated the links from notion to relative links * Added a routes manifest to the docs * Removed the <br> after examples toggle * Use the name of the section instead of Introduction * Fixed spelling errors * Optimize the content for Algolia * Add a paragraph for `pageProps` * Add welcome section * Transpile -> Compile * Update getting-started.md * Update getting-started.md * Update getting-started.md * Update getting-started.md * Update getting-started.md * Update getting-started.md * Update getting-started.md * Update getting-started.md * Update getting-started.md * Update getting-started.md * Test extra room between * Update getting-started.md * Update getting-started.md * Update getting-started.md * Update getting-started.md * Update getting-started.md * Update manifest.json * Update getting-started.md * Update getting-started.md * Add concepts section * Update pages.md * Update pages.md * Add data fetching section * Update pages.md * See how a card looks like * Undo card changes * Added related section to getting-started * Fixed wrong markdown syntax in the withRouter page * Moved the server-side-and-client-side section * Updated next-cli reference * updated getInitialProps section * Minor fixes * Added more Related sections * Add html to the related section in getting-started * Use small for the card * Use cards for all related sections * Added src directory docs * Added src directory to the manifest * Add note about API routes in next export * Add initial data fetching docs (private until new methods are released) * Fix typos * Improve wording * Update getting-started.md * Update getting-started.md * Move advanced concepts to advanced section * Hide server-side vs client-side * Move AMP support * Move typescript into one page * Add routing concepts page * Remove introduction page * Update section on different route types * Update routing.md * Update routing.md * Update routing.md * Update routing.md * Combine router injection pages * Update pages.md * Update routing.md * Update using-link.md * Update using-link.md * Update typescript.md * Move the API Routes typescript to basic features * Added links to the typescript section * Updated links to useRouter and withRouter * Add singleLevel prop to manifest * Added single page for router docs * Updated description * Updated the routes in the manifest * Add data fetching section * Update data-fetching.md * Update data-fetching.md * Update dynamic-routes.md * Update manifest.json * Only use the single router API page * Moved the concepts pages * Updated links * Removed extra space * Updated title for Router API * Added a description with frontmatter * Add open prop to the manifest * Added datafetching section to API Reference * Updated links to the getInitialProps reference * Moved some sections to API * Added next/head to API reference * Added next/link to the API Reference * Removed the populating-head section * Updated links to the new next/link API * Added link from dynamic-routes to next/link docs * use a paragraph * Added next/router API * Added next/amp * Updated the docs for next/amp * Moved the AMP support folder * Updated title * Content updates * Added more links to the data fetching section * Added links from the API to introductions * changing the router API * Updates to the router API * Updated the routing section * life improvements * Added shallow routing section * Small fix * Removed old routing sections * Updated link to shallow routing * Removed unrequired page * Removed /pages * Update data-fetching.md * Add initial deployments section * Update manifest.json * Update introduction.md * Update deployment doc * Add static export section updates * link ssg/ssr * Update deployment.md * Add syntax highlighting Co-authored-by: Tim Neutkens <tim@timneutkens.nl>
2019-12-23 16:07:38 +01:00
### TypeScript
If youre using TypeScript, take a look at [our TypeScript documentation](/docs/basic-features/typescript.md#custom-app).
New docs (#9301) * Added the docs from Notion * Updated the links from notion to relative links * Added a routes manifest to the docs * Removed the <br> after examples toggle * Use the name of the section instead of Introduction * Fixed spelling errors * Optimize the content for Algolia * Add a paragraph for `pageProps` * Add welcome section * Transpile -> Compile * Update getting-started.md * Update getting-started.md * Update getting-started.md * Update getting-started.md * Update getting-started.md * Update getting-started.md * Update getting-started.md * Update getting-started.md * Update getting-started.md * Update getting-started.md * Test extra room between * Update getting-started.md * Update getting-started.md * Update getting-started.md * Update getting-started.md * Update getting-started.md * Update manifest.json * Update getting-started.md * Update getting-started.md * Add concepts section * Update pages.md * Update pages.md * Add data fetching section * Update pages.md * See how a card looks like * Undo card changes * Added related section to getting-started * Fixed wrong markdown syntax in the withRouter page * Moved the server-side-and-client-side section * Updated next-cli reference * updated getInitialProps section * Minor fixes * Added more Related sections * Add html to the related section in getting-started * Use small for the card * Use cards for all related sections * Added src directory docs * Added src directory to the manifest * Add note about API routes in next export * Add initial data fetching docs (private until new methods are released) * Fix typos * Improve wording * Update getting-started.md * Update getting-started.md * Move advanced concepts to advanced section * Hide server-side vs client-side * Move AMP support * Move typescript into one page * Add routing concepts page * Remove introduction page * Update section on different route types * Update routing.md * Update routing.md * Update routing.md * Update routing.md * Combine router injection pages * Update pages.md * Update routing.md * Update using-link.md * Update using-link.md * Update typescript.md * Move the API Routes typescript to basic features * Added links to the typescript section * Updated links to useRouter and withRouter * Add singleLevel prop to manifest * Added single page for router docs * Updated description * Updated the routes in the manifest * Add data fetching section * Update data-fetching.md * Update data-fetching.md * Update dynamic-routes.md * Update manifest.json * Only use the single router API page * Moved the concepts pages * Updated links * Removed extra space * Updated title for Router API * Added a description with frontmatter * Add open prop to the manifest * Added datafetching section to API Reference * Updated links to the getInitialProps reference * Moved some sections to API * Added next/head to API reference * Added next/link to the API Reference * Removed the populating-head section * Updated links to the new next/link API * Added link from dynamic-routes to next/link docs * use a paragraph * Added next/router API * Added next/amp * Updated the docs for next/amp * Moved the AMP support folder * Updated title * Content updates * Added more links to the data fetching section * Added links from the API to introductions * changing the router API * Updates to the router API * Updated the routing section * life improvements * Added shallow routing section * Small fix * Removed old routing sections * Updated link to shallow routing * Removed unrequired page * Removed /pages * Update data-fetching.md * Add initial deployments section * Update manifest.json * Update introduction.md * Update deployment doc * Add static export section updates * link ssg/ssr * Update deployment.md * Add syntax highlighting Co-authored-by: Tim Neutkens <tim@timneutkens.nl>
2019-12-23 16:07:38 +01:00
## Related
For more information on what to do next, we recommend the following sections:
<div class="card">
<a href="/docs/advanced-features/automatic-static-optimization.md">
<b>Automatic Static Optimization:</b>
<small>Next.js automatically optimizes your app to be static HTML whenever possible. Learn how it works here.</small>
New docs (#9301) * Added the docs from Notion * Updated the links from notion to relative links * Added a routes manifest to the docs * Removed the <br> after examples toggle * Use the name of the section instead of Introduction * Fixed spelling errors * Optimize the content for Algolia * Add a paragraph for `pageProps` * Add welcome section * Transpile -> Compile * Update getting-started.md * Update getting-started.md * Update getting-started.md * Update getting-started.md * Update getting-started.md * Update getting-started.md * Update getting-started.md * Update getting-started.md * Update getting-started.md * Update getting-started.md * Test extra room between * Update getting-started.md * Update getting-started.md * Update getting-started.md * Update getting-started.md * Update getting-started.md * Update manifest.json * Update getting-started.md * Update getting-started.md * Add concepts section * Update pages.md * Update pages.md * Add data fetching section * Update pages.md * See how a card looks like * Undo card changes * Added related section to getting-started * Fixed wrong markdown syntax in the withRouter page * Moved the server-side-and-client-side section * Updated next-cli reference * updated getInitialProps section * Minor fixes * Added more Related sections * Add html to the related section in getting-started * Use small for the card * Use cards for all related sections * Added src directory docs * Added src directory to the manifest * Add note about API routes in next export * Add initial data fetching docs (private until new methods are released) * Fix typos * Improve wording * Update getting-started.md * Update getting-started.md * Move advanced concepts to advanced section * Hide server-side vs client-side * Move AMP support * Move typescript into one page * Add routing concepts page * Remove introduction page * Update section on different route types * Update routing.md * Update routing.md * Update routing.md * Update routing.md * Combine router injection pages * Update pages.md * Update routing.md * Update using-link.md * Update using-link.md * Update typescript.md * Move the API Routes typescript to basic features * Added links to the typescript section * Updated links to useRouter and withRouter * Add singleLevel prop to manifest * Added single page for router docs * Updated description * Updated the routes in the manifest * Add data fetching section * Update data-fetching.md * Update data-fetching.md * Update dynamic-routes.md * Update manifest.json * Only use the single router API page * Moved the concepts pages * Updated links * Removed extra space * Updated title for Router API * Added a description with frontmatter * Add open prop to the manifest * Added datafetching section to API Reference * Updated links to the getInitialProps reference * Moved some sections to API * Added next/head to API reference * Added next/link to the API Reference * Removed the populating-head section * Updated links to the new next/link API * Added link from dynamic-routes to next/link docs * use a paragraph * Added next/router API * Added next/amp * Updated the docs for next/amp * Moved the AMP support folder * Updated title * Content updates * Added more links to the data fetching section * Added links from the API to introductions * changing the router API * Updates to the router API * Updated the routing section * life improvements * Added shallow routing section * Small fix * Removed old routing sections * Updated link to shallow routing * Removed unrequired page * Removed /pages * Update data-fetching.md * Add initial deployments section * Update manifest.json * Update introduction.md * Update deployment doc * Add static export section updates * link ssg/ssr * Update deployment.md * Add syntax highlighting Co-authored-by: Tim Neutkens <tim@timneutkens.nl>
2019-12-23 16:07:38 +01:00
</a>
</div>
<div class="card">
<a href="/docs/advanced-features/custom-error-page.md">
<b>Custom Error Page:</b>
<small>Learn more about the built-in Error page.</small>
</a>
</div>