Next.js _document.js example

Use _document.js to customize the HTML document in Next.js.

When using Next.js you may need to change the HTML that wraps every page, like adding a lang attribute to the <html> tag, loading a font from Google Fonts or adding a class to the <body>. Your page components can't reach those tags. This can be done by creating a _document.js file directly in the ./pages/ folder. If this file exists, Next.js will use it instead of the default Document.

_app.js wraps your pages. _document.js wraps your whole app in the HTML document.

If you haven't seen _app.js yet, take a look at the Next.js _app.js example first.

A basic example of _document.js

A custom Document is a class that extends Document from next/document. This is the default with no extended functionality:

// pages/_document.js
import Document, { Html, Head, Main, NextScript } from "next/document"
class MyDocument extends Document {
static async getInitialProps(ctx) {
const initialProps = await Document.getInitialProps(ctx)
return { ...initialProps }
}
render() {
return (
<Html>
<Head />
<body>
<Main />
<NextScript />
</body>
</Html>
)
}
}
export default MyDocument

Html, Head, Main and NextScript are all required for the page to render properly. Main is where your app (and _app.js) is rendered, and NextScript adds the scripts Next.js needs to run your app in the browser.

The getInitialProps method calls Document.getInitialProps(ctx) and returns the result. You only need it if you want to change what gets passed to the Document, so you can leave it out of the basic example. It comes in handy for CSS-in-JS libraries like styled-components that need to collect styles on the server.

Setting the lang attribute

Screen readers use the lang attribute to know how to pronounce the page, and it's one of the first things an accessibility audit will flag. Add it to the Html component:

// pages/_document.js
...
<Html lang="en">
...

Adding fonts and body classes with _document.js

Anything inside Head is added to the <head> of every page. This is a good place for font <link> tags:

// pages/_document.js
import Document, { Html, Head, Main, NextScript } from "next/document"
class MyDocument extends Document {
render() {
return (
<Html lang="en">
<Head>
<link rel="preconnect" href="https://fonts.gstatic.com" />
<link
href="https://fonts.googleapis.com/css2?family=Inter:wght@400;700&display=swap"
rel="stylesheet"
/>
</Head>
<body className="bg-gray-100 antialiased">
<Main />
<NextScript />
</body>
</Html>
)
}
}
export default MyDocument

The className on body is applied to every page. This is handy when a CSS framework or a theme needs a class on the <body> itself.

Adding third-party scripts with _document.js

Scripts that need to load on every page, like analytics, can also go in Head. Inline scripts need dangerouslySetInnerHTML, because React won't render the contents of a <script> tag on its own:

// pages/_document.js
...
<Head>
<script
async
src="https://www.googletagmanager.com/gtag/js?id=UA-XXXXX-Y"
/>
<script
dangerouslySetInnerHTML={{
__html: `
window.dataLayer = window.dataLayer || [];
function gtag(){dataLayer.push(arguments);}
gtag('js', new Date());
gtag('config', 'UA-XXXXX-Y');
`,
}}
/>
</Head>
...

What goes in _document.js vs _app.js?

The Document is only rendered on the server. It never runs in the browser, so event handlers like onClick won't work and browser APIs like window aren't available. React state won't work here either.

  • _document.js: the lang attribute, font and <link> tags, third-party scripts, body classes.
  • _app.js: layouts, global CSS imports, providers and state that persists between pages.

Note: The Head from next/document is not the same as the Head from next/head. Use next/document's Head only in _document.js for tags shared by every page. Page titles and per-page meta tags belong in next/head inside your page components.

Check out the Next.js docs for more on the custom Document.

Have some feedback on this post? .