Back to Biomejs

noHeadElement

src/content/docs/linter/rules/no-head-element.mdx

latest3.7 KB
Original Source

import { Tabs, TabItem } from '@astrojs/starlight/components';

<Tabs> <TabItem label="JSX and TSX" icon="seti:javascript"> ## Summary - Rule available since: `v1.9.4` - Diagnostic Category: [`lint/style/noHeadElement`](/reference/diagnostics#diagnostic-category) - This rule is **recommended**, meaning it is enabled by default. - This rule doesn't have a fix. - The default severity of this rule is [**warning**](/reference/diagnostics#warning). - This rule belongs to the following domains: - [`next`](/linter/domains#next) - Sources: - Same as [`@next/next/no-head-element`](https://nextjs.org/docs/messages/no-head-element)

How to configure

json
{
	"linter": {
		"rules": {
			"style": {
				"noHeadElement": "error"
			}
		}
	}
}

Description

Prevent usage of <head> element in a Next.js project.

Next.js provides a specialized <Head /> component from next/head that manages the <head> tag for optimal server-side rendering, client-side navigation, and automatic deduplication of tags such as <meta> and <title>.

This rule only checks files that are outside of the app/ directory, as it's typically handled differently in Next.js.

Examples

Invalid

jsx
function Index() {
  return (
    <head>
      <title>Invalid</title>
    </head>
  )
}
<pre class="language-text"><code class="language-text">code-block.jsx:2:11 <a href="https://biomejs.dev/linter/rules/no-head-element">lint/style/noHeadElement</a> ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ <strong><span style="color: Orange;">⚠</span></strong> <span style="color: Orange;">Don't use </span><span style="color: Orange;"><strong>&lt;head&gt;</strong></span><span style="color: Orange;"> element.</span> <strong>1 │ </strong>function Index() &#123; <strong><span style="color: Tomato;">&gt;</span></strong> <strong>2 │ </strong> return ( <strong> │ </strong> <strong><span style="color: Tomato;">&gt;</span></strong> <strong>3 │ </strong> &lt;head&gt; <strong> │ </strong> <strong><span style="color: Tomato;">^</span></strong><strong><span style="color: Tomato;">^</span></strong><strong><span style="color: Tomato;">^</span></strong><strong><span style="color: Tomato;">^</span></strong><strong><span style="color: Tomato;">^</span></strong><strong><span style="color: Tomato;">^</span></strong> <strong>4 │ </strong> &lt;title&gt;Invalid&lt;/title&gt; <strong>5 │ </strong> &lt;/head&gt; <strong><span style="color: lightgreen;">ℹ</span></strong> <span style="color: lightgreen;">Using the </span><span style="color: lightgreen;"><strong>&lt;head&gt;</strong></span><span style="color: lightgreen;"> element can cause unexpected behavior in a Next.js application. Use </span><span style="color: lightgreen;"><strong>&lt;Head /&gt;</strong></span><span style="color: lightgreen;"> from </span><span style="color: lightgreen;"><strong>next/head</strong></span><span style="color: lightgreen;"> instead.</span> </code></pre>

Valid

jsx
import Head from 'next/head'

function Index() {
  return (
    <Head>
      <title>All good!</title>
    </Head>
  )
}
</TabItem> </Tabs>