-
Notifications
You must be signed in to change notification settings - Fork 27k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
adds no duplicate head rule (#27179)
- Loading branch information
1 parent
d29a95a
commit c46b405
Showing
5 changed files
with
241 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
# No Duplicate Head | ||
|
||
### Why This Error Occurred | ||
|
||
More than a single instance of the `<Head />` component was used in a single custom document. This can cause unexpected behavior in your application. | ||
|
||
### Possible Ways to Fix It | ||
|
||
Only use a single `<Head />` component in your custom document in `pages/_document.js`. | ||
|
||
```jsx | ||
// pages/_document.js | ||
import Document, { Html, Head, Main, NextScript } from 'next/document' | ||
|
||
class MyDocument extends Document { | ||
static async getInitialProps(ctx) { | ||
//... | ||
} | ||
|
||
render() { | ||
return ( | ||
<Html> | ||
<Head /> | ||
<body> | ||
<Main /> | ||
<NextScript /> | ||
</body> | ||
</Html> | ||
) | ||
} | ||
} | ||
|
||
export default MyDocument | ||
``` | ||
|
||
### Useful Links | ||
|
||
- [Custom Document](https://nextjs.org/docs/advanced-features/custom-document) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
55 changes: 55 additions & 0 deletions
55
packages/eslint-plugin-next/lib/rules/no-duplicate-head.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
module.exports = { | ||
meta: { | ||
docs: { | ||
description: 'Enforce no duplicate usage of <Head> in pages/document.js', | ||
recommended: true, | ||
}, | ||
}, | ||
create: function (context) { | ||
let documentImportName | ||
return { | ||
ImportDeclaration(node) { | ||
if (node.source.value === 'next/document') { | ||
const documentImport = node.specifiers.find( | ||
({ type }) => type === 'ImportDefaultSpecifier' | ||
) | ||
if (documentImport && documentImport.local) { | ||
documentImportName = documentImport.local.name | ||
} | ||
} | ||
}, | ||
ReturnStatement(node) { | ||
const ancestors = context.getAncestors() | ||
const documentClass = ancestors.find( | ||
(ancestorNode) => | ||
ancestorNode.type === 'ClassDeclaration' && | ||
ancestorNode.superClass && | ||
ancestorNode.superClass.name === documentImportName | ||
) | ||
|
||
if (!documentClass) { | ||
return | ||
} | ||
|
||
if (node.argument && node.argument.children) { | ||
const headComponents = node.argument.children.filter( | ||
(childrenNode) => | ||
childrenNode.openingElement && | ||
childrenNode.openingElement.name && | ||
childrenNode.openingElement.name.name === 'Head' | ||
) | ||
|
||
if (headComponents.length > 1) { | ||
for (let i = 1; i < headComponents.length; i++) { | ||
context.report({ | ||
node: headComponents[i], | ||
message: | ||
'Do not include multiple instances of <Head/>. See: https://nextjs.org/docs/messages/no-duplicate-head', | ||
}) | ||
} | ||
} | ||
} | ||
}, | ||
} | ||
}, | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,142 @@ | ||
const rule = require('@next/eslint-plugin-next/lib/rules/no-duplicate-head') | ||
|
||
const RuleTester = require('eslint').RuleTester | ||
|
||
RuleTester.setDefaultConfig({ | ||
parserOptions: { | ||
ecmaVersion: 2018, | ||
sourceType: 'module', | ||
ecmaFeatures: { | ||
modules: true, | ||
jsx: true, | ||
}, | ||
}, | ||
}) | ||
|
||
var ruleTester = new RuleTester() | ||
ruleTester.run('no-duplicate-head', rule, { | ||
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: | ||
'Do not include multiple instances of <Head/>. See: https://nextjs.org/docs/messages/no-duplicate-head', | ||
type: 'JSXElement', | ||
}, | ||
{ | ||
message: | ||
'Do not include multiple instances of <Head/>. See: https://nextjs.org/docs/messages/no-duplicate-head', | ||
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: | ||
'Do not include multiple instances of <Head/>. See: https://nextjs.org/docs/messages/no-duplicate-head', | ||
type: 'JSXElement', | ||
}, | ||
], | ||
}, | ||
], | ||
}) |