no-head-import-in-document.js 987 B

1234567891011121314151617181920212223242526272829303132333435363738394041
  1. const path = require('path')
  2. const url = 'https://nextjs.org/docs/messages/no-head-import-in-document'
  3. module.exports = {
  4. meta: {
  5. docs: {
  6. description: 'Prevent usage of `next/head` in `pages/_document.js`.',
  7. recommended: true,
  8. url,
  9. },
  10. type: 'problem',
  11. schema: [],
  12. },
  13. create: function (context) {
  14. return {
  15. ImportDeclaration(node) {
  16. if (node.source.value !== 'next/head') {
  17. return
  18. }
  19. const document = context.getFilename().split('pages')[1]
  20. if (!document) {
  21. return
  22. }
  23. const { name, dir } = path.parse(document)
  24. if (
  25. name.startsWith('_document') ||
  26. (dir === '/_document' && name === 'index')
  27. ) {
  28. context.report({
  29. node,
  30. message: `\`next/head\` should not be imported in \`pages${document}\`. Use \`<Head />\` from \`next/document\` instead. See: ${url}`,
  31. })
  32. }
  33. },
  34. }
  35. },
  36. }