Skip to content
← Back to rules

nextjs/no-duplicate-head Correctness

它的作用

防止在 pages/_document.js 中重复使用 <Head>

为什么这有问题?

这可能会导致你的应用中出现意外行为。

示例

以下是此规则的错误代码示例:

jsx
import Document, { Html, Head, Main, NextScript } from "next/document";
class MyDocument extends Document {
  static async getInitialProps(ctx) {}
  render() {
    return (
      <Html>
        <Head />
        <Head />
        <body>
          <Main />
          <NextScript />
        </body>
      </Html>
    );
  }
}
export default MyDocument;

以下是此规则的正确代码示例:

jsx
import Document, { Html, Head, Main, NextScript } from "next/document";
class MyDocument extends Document {
  static async getInitialProps(ctx) {}
  render() {
    return (
      <Html>
        <Head />
        <body>
          <Main />
          <NextScript />
        </body>
      </Html>
    );
  }
}
export default MyDocument;

如何使用

To enable this rule using the config file or in the CLI, you can use:

json
{
  "plugins": ["nextjs"],
  "rules": {
    "nextjs/no-duplicate-head": "error"
  }
}
ts
import { defineConfig } from "oxlint";

export default defineConfig({
  plugins: ["nextjs"],
  rules: {
    "nextjs/no-duplicate-head": "error",
  },
});
bash
oxlint --deny nextjs/no-duplicate-head --nextjs-plugin

版本

此规则于 v0.3.3 中添加。

参考资料