12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455 |
- const url = 'https://nextjs.org/docs/messages/no-script-component-in-head'
- module.exports = {
- meta: {
- docs: {
- description: 'Prevent usage of `next/script` in `next/head` component.',
- recommended: true,
- url,
- },
- type: 'problem',
- schema: [],
- },
- create: function (context) {
- let isNextHead = null
- return {
- ImportDeclaration(node) {
- if (node.source.value === 'next/head') {
- isNextHead = node.source.value
- }
- if (node.source.value !== 'next/script') {
- return
- }
- },
- JSXElement(node) {
- if (!isNextHead) {
- return
- }
- if (
- node.openingElement &&
- node.openingElement.name &&
- node.openingElement.name.name !== 'Head'
- ) {
- return
- }
- const scriptTag = node.children.find(
- (child) =>
- child.openingElement &&
- child.openingElement.name &&
- child.openingElement.name.name === 'Script'
- )
- if (scriptTag) {
- context.report({
- node,
- message: `\`next/script\` should not be used in \`next/head\` component. Move \`<Script />\` outside of \`<Head>\` instead. See: ${url}`,
- })
- }
- },
- }
- },
- }
|