Skip to content
← Back to rules

nextjs/no-head-import-in-document Correctness

它的作用

阻止在 Next.js 文档中使用 next/head

为什么这不好?

pages/_document.js 中导入 next/head 可能会在你的 Next.js 应用程序中导致 意外问题。

示例

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

jsx
import Document, { Html, Main, NextScript } from "next/document";
import Head from "next/head";

class MyDocument extends Document {
  static async getInitialProps(ctx) {
    //...
  }

  render() {
    return (
      <Html>
        <Head></Head>
      </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></Head>
      </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-head-import-in-document": "error"
  }
}
ts
import { defineConfig } from "oxlint";

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

版本

此规则在 v0.2.0 中添加。

参考资料