Custom Document

A custom Document can update the <html> and <body> tags used to render a Page. This file is only rendered on the server, so event handlers like onClick cannot be used in _document.

To override the default Document, create the file pages/_document.js as shown below:

  1. import { Html, Head, Main, NextScript } from 'next/document'
  2. export default function Document() {
  3. return (
  4. <Html>
  5. <Head />
  6. <body>
  7. <Main />
  8. <NextScript />
  9. </body>
  10. </Html>
  11. )
  12. }

The code above is the default Document added by Next.js. Custom attributes are allowed as props. For example, we might want to add lang="en" to the <html> tag:

  1. <Html lang="en">

Or add a className to the body tag:

  1. <body className="bg-white">

<Html>, <Head />, <Main /> and <NextScript /> are required for the page to be properly rendered.

Caveats

  • The <Head /> component used in _document is not the same as next/head. The <Head /> component used here should only be used for any <head> code that is common for all pages. For all other cases, such as <title> tags, we recommend using next/head in your pages or components.
  • React components outside of <Main /> will not be initialized by the browser. Do not add application logic here or custom CSS (like styled-jsx). If you need shared components in all your pages (like a menu or a toolbar), read Layouts instead.
  • Document currently does not support Next.js Data Fetching methods like getStaticProps or getServerSideProps.

Customizing renderPage

Note: This is advanced and only needed for libraries like CSS-in-JS to support server-side rendering. This is not needed for built-in styled-jsx support.

To prepare for React 18, we recommend avoiding customizing getInitialProps and renderPage, if possible.

The ctx object shown below is equivalent to the one received in getInitialProps, with the addition of renderPage.

  1. import Document, { Html, Head, Main, NextScript } from 'next/document'
  2. class MyDocument extends Document {
  3. static async getInitialProps(ctx) {
  4. const originalRenderPage = ctx.renderPage
  5. // Run the React rendering logic synchronously
  6. ctx.renderPage = () =>
  7. originalRenderPage({
  8. // Useful for wrapping the whole react tree
  9. enhanceApp: (App) => App,
  10. // Useful for wrapping in a per-page basis
  11. enhanceComponent: (Component) => Component,
  12. })
  13. // Run the parent `getInitialProps`, it now includes the custom `renderPage`
  14. const initialProps = await Document.getInitialProps(ctx)
  15. return initialProps
  16. }
  17. render() {
  18. return (
  19. <Html>
  20. <Head />
  21. <body>
  22. <Main />
  23. <NextScript />
  24. </body>
  25. </Html>
  26. )
  27. }
  28. }
  29. export default MyDocument

Note: getInitialProps in _document is not called during client-side transitions.

TypeScript

You can use the built-in DocumentContext type and change the file name to ./pages/_document.tsx like so:

  1. import Document, { DocumentContext, DocumentInitialProps } from 'next/document'
  2. class MyDocument extends Document {
  3. static async getInitialProps(
  4. ctx: DocumentContext
  5. ): Promise<DocumentInitialProps> {
  6. const initialProps = await Document.getInitialProps(ctx)
  7. return initialProps
  8. }
  9. }
  10. export default MyDocument