Skip to content
← Back to rules

nextjs/no-head-element 正确性

作用

禁止在 Next.js 应用中使用原生的 <head> 元素。

为什么这不好?

<head> 元素可能会在 Next.js 应用中引发意外行为。 请改用 Next.js 内置的 next/head 组件。

示例

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

jsx
function Index() {
  return (
    <>
      <head>
        <title>My page title</title>
        <meta name="viewport" content="initial-scale=1.0, width=device-width" />
      </head>
    </>
  );
}

export default Index;

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

jsx
import Head from "next/head";

function Index() {
  return (
    <>
      <Head>
        <title>My page title</title>
        <meta name="viewport" content="initial-scale=1.0, width=device-width" />
      </Head>
    </>
  );
}

export default Index;

如何使用

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

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

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

版本

此规则于 v0.2.1 中添加。

参考