Menu

Custom Document

自定义 Document 可以更新用于渲染页面<html><body> 标签。

要覆盖默认的 Document,请创建文件 pages/_document,如下所示:

pages/_document.tsx
TypeScript
import { Html, Head, Main, NextScript } from 'next/document'
 
export default function Document() {
  return (
    <Html lang="en">
      <Head />
      <body>
        <Main />
        <NextScript />
      </body>
    </Html>
  )
}

值得注意的是

  • _document 仅在服务器上渲染,因此像 onClick 这样的事件处理程序不能在此文件中使用。
  • <Html><Head /><Main /><NextScript /> 是页面正确渲染所必需的。

注意事项

  • _document 中使用的 <Head /> 组件与 next/head 不同。这里使用的 <Head /> 组件应该仅用于所有页面通用的 <head> 代码。对于所有其他情况,例如 <title> 标签,我们建议在你的页面或组件中使用 next/head
  • <Main /> 之外的 React 组件不会被浏览器初始化。不要在这里添加应用程序逻辑或自定义 CSS(如 styled-jsx)。如果你需要在所有页面中共享组件(如菜单或工具栏),请阅读布局
  • Document 目前不支持 Next.js 数据获取方法,如 getStaticPropsgetServerSideProps

自定义 renderPage

自定义 renderPage 是高级功能,仅在像 CSS-in-JS 这样的库需要支持服务器端渲染时才需要。内置的 styled-jsx 支持不需要这样做。

我们不建议使用此模式。 相反,请考虑逐步采用 App Router,它可以让你更轻松地为页面和布局获取数据。

pages/_document.tsx
TypeScript
import Document, {
  Html,
  Head,
  Main,
  NextScript,
  DocumentContext,
  DocumentInitialProps,
} from 'next/document'
 
class MyDocument extends Document {
  static async getInitialProps(
    ctx: DocumentContext
  ): Promise<DocumentInitialProps> {
    const originalRenderPage = ctx.renderPage
 
    // 同步运行 React 渲染逻辑
    ctx.renderPage = () =>
      originalRenderPage({
        // 用于包装整个 react 树
        enhanceApp: (App) => App,
        // 用于基于每个页面的包装
        enhanceComponent: (Component) => Component,
      })
 
    // 运行父级 `getInitialProps`,它现在包含自定义的 `renderPage`
    const initialProps = await Document.getInitialProps(ctx)
 
    return initialProps
  }
 
  render() {
    return (
      <Html lang="en">
        <Head />
        <body>
          <Main />
          <NextScript />
        </body>
      </Html>
    )
  }
}
 
export default MyDocument

值得注意的是

  • _document 中的 getInitialProps 在客户端转换期间不会被调用。
  • _documentctx 对象等同于 getInitialProps 中接收到的对象,另外还包含了 renderPage