mirror of
https://github.com/hanzoai/next.js.git
synced 2026-09-24 17:00:51 +00:00
ESLint plugin defaults to flat config, and we also dropped the built-in lint check at https://github.com/vercel/next.js/pull/83136, so we can sunset the deprecated ESLint v8 testing.
147 lines
3.2 KiB
TypeScript
147 lines
3.2 KiB
TypeScript
import { RuleTester } from 'eslint'
|
|
import { rules } from '@next/eslint-plugin-next'
|
|
|
|
const NextESLintRule = rules['no-duplicate-head']
|
|
|
|
const message =
|
|
'Do not include multiple instances of `<Head/>`. See: https://nextjs.org/docs/messages/no-duplicate-head'
|
|
|
|
const tests = {
|
|
valid: [
|
|
{
|
|
code: `import Document, { Html, Head, Main, NextScript } from 'next/document'
|
|
|
|
class MyDocument extends Document {
|
|
static async getInitialProps(ctx) {
|
|
//...
|
|
}
|
|
|
|
render() {
|
|
return (
|
|
<Html>
|
|
<Head/>
|
|
</Html>
|
|
)
|
|
}
|
|
}
|
|
|
|
export default MyDocument
|
|
`,
|
|
filename: 'pages/_document.js',
|
|
},
|
|
{
|
|
code: `import Document, { Html, Head, Main, NextScript } from 'next/document'
|
|
|
|
class MyDocument extends Document {
|
|
render() {
|
|
return (
|
|
<Html>
|
|
<Head>
|
|
<meta charSet="utf-8" />
|
|
<link
|
|
href="https://fonts.googleapis.com/css2?family=Sarabun:ital,wght@0,400;0,700;1,400;1,700&display=swap"
|
|
rel="stylesheet"
|
|
/>
|
|
</Head>
|
|
</Html>
|
|
)
|
|
}
|
|
}
|
|
|
|
export default MyDocument
|
|
`,
|
|
filename: 'pages/_document.tsx',
|
|
},
|
|
],
|
|
invalid: [
|
|
{
|
|
code: `
|
|
import Document, { Html, Main, NextScript } from 'next/document'
|
|
import Head from 'next/head'
|
|
|
|
class MyDocument extends Document {
|
|
render() {
|
|
return (
|
|
<Html>
|
|
<Head />
|
|
<Head />
|
|
<Head />
|
|
</Html>
|
|
)
|
|
}
|
|
}
|
|
|
|
export default MyDocument
|
|
`,
|
|
filename: 'pages/_document.js',
|
|
errors: [
|
|
{
|
|
message,
|
|
type: 'JSXElement',
|
|
},
|
|
{
|
|
message,
|
|
type: 'JSXElement',
|
|
},
|
|
],
|
|
},
|
|
{
|
|
code: `
|
|
import Document, { Html, Main, NextScript } from 'next/document'
|
|
import Head from 'next/head'
|
|
|
|
class MyDocument extends Document {
|
|
render() {
|
|
return (
|
|
<Html>
|
|
<Head>
|
|
<meta charSet="utf-8" />
|
|
<link
|
|
href="https://fonts.googleapis.com/css2?family=Sarabun:ital,wght@0,400;0,700;1,400;1,700&display=swap"
|
|
rel="stylesheet"
|
|
/>
|
|
</Head>
|
|
<body>
|
|
<Main />
|
|
<NextScript />
|
|
</body>
|
|
<Head>
|
|
<script
|
|
dangerouslySetInnerHTML={{
|
|
__html: '',
|
|
}}
|
|
/>
|
|
</Head>
|
|
</Html>
|
|
)
|
|
}
|
|
}
|
|
|
|
export default MyDocument
|
|
`,
|
|
filename: 'pages/_document.page.tsx',
|
|
errors: [
|
|
{
|
|
message,
|
|
type: 'JSXElement',
|
|
},
|
|
],
|
|
},
|
|
],
|
|
}
|
|
|
|
describe('no-duplicate-head', () => {
|
|
new RuleTester({
|
|
languageOptions: {
|
|
ecmaVersion: 2018,
|
|
sourceType: 'module',
|
|
parserOptions: {
|
|
ecmaFeatures: {
|
|
modules: true,
|
|
jsx: true,
|
|
},
|
|
},
|
|
},
|
|
}).run('eslint', NextESLintRule, tests)
|
|
})
|