Skip to content

noHeadImportInDocument

¥Summary

¥How to configure

biome.json
{
"linter": {
"rules": {
"suspicious": {
"noHeadImportInDocument": "error"
}
}
}
}

¥Description

防止在 Next.js 项目中使用 pages/_document.js 中的 next/head 模块。

¥Prevent using the next/head module in pages/_document.js on Next.js projects.

在自定义 pages/_document.js 文件中导入 next/head 可能会导致应用出现意外行为。next/head 组件设计用于页面级别,在自定义文档中使用时,可能会干扰全局文档结构,从而导致渲染和 SEO 问题。

¥Importing next/head within the custom pages/_document.js file can cause unexpected behavior in your application. The next/head component is designed to be used at the page level, and when used in the custom document it can interfere with the global document structure, which leads to issues with rendering and SEO.

要修改所有页面上的 <head> 元素,你应该使用 next/document 模块中的 <Head /> 组件。

¥To modify <head> elements across all pages, you should use the <Head /> component from the next/document module.

¥Examples

¥Valid

pages/_document.js
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;

¥Related links