0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

Next.jsのDocumentが理解出来てなかったのでメモっておく

Posted at

Documentとは

ページがレンダリングされるときに タグと

タグを拡張するために使用される。
このファイルはサーバーサイドでのみレンダリングされます。
そのためonClickなどのイベントハンドラーはDocumentで利用できない。

使い方

Next.jsのpagesコンポーネントは、デフォルトで

、を定義してくれるが、これらを変更したい場合はpages/_document.jsを使う。
この時にDocumentをextendsする。
Html, Head, Main, NextScriptのいずれかが不足しているとエラーが発生します。
import Document, { Html, Head, Main, NextScript } from 'next/document'

class MyDocument extends Document {
  render() {
    return (
      <Html>
        <Head>
          <link href="https://fonts.googleapis.com/css?family=Noto+Sans+JP" rel="stylesheet" />
        </Head>
        <body>
          <Main />
          <NextScript />
        </body>
      </Html>
    )
  }
}

参考

Next.jsの_document.jsを理解する
Custom Document
[カスタム Document(https://nextjs-ja-translation-docs.vercel.app/docs/advanced-features/custom-document)
Next.jsのnext/headとnext/documentについて

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?