Skip to content
← Back to rules

nextjs/no-title-in-document-head 正确性

它的作用

阻止在 next/documentHead 组件中使用 <title>

为什么这是不好的?

<title> 元素应仅用于所有页面共用的 <head> 代码。 标题标签应在页面级别使用 next/head 来定义,而不是在 next/document 中。

示例

此规则的错误代码示例:

javascript
import { Head } from "next/document";

export function Home() {
  return (
    <div>
      <Head>
        <title>我的页面标题</title>
      </Head>
    </div>
  );
}

此规则的正确代码示例:

javascript
import Head from "next/head";

export function Home() {
  return (
    <div>
      <Head>
        <title>我的页面标题</title>
      </Head>
    </div>
  );
}

如何使用

要通过配置文件或 CLI 启用此规则,可以使用:

json
{
  "plugins": ["nextjs"],
  "rules": {
    "nextjs/no-title-in-document-head": "error"
  }
}
bash
oxlint --deny nextjs/no-title-in-document-head --nextjs-plugin

参考资料