google-font-preconnect.js 1.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  1. const NodeAttributes = require('../utils/node-attributes.js')
  2. const url = 'https://nextjs.org/docs/messages/google-font-preconnect'
  3. module.exports = {
  4. meta: {
  5. docs: {
  6. description: 'Ensure `preconnect` is used with Google Fonts.',
  7. recommended: true,
  8. url,
  9. },
  10. type: 'problem',
  11. schema: [],
  12. },
  13. create: function (context) {
  14. return {
  15. JSXOpeningElement(node) {
  16. if (node.name.name !== 'link') {
  17. return
  18. }
  19. const attributes = new NodeAttributes(node)
  20. if (!attributes.has('href') || !attributes.hasValue('href')) {
  21. return
  22. }
  23. const hrefValue = attributes.value('href')
  24. const preconnectMissing =
  25. !attributes.has('rel') ||
  26. !attributes.hasValue('rel') ||
  27. attributes.value('rel') !== 'preconnect'
  28. if (
  29. typeof hrefValue === 'string' &&
  30. hrefValue.startsWith('https://fonts.gstatic.com') &&
  31. preconnectMissing
  32. ) {
  33. context.report({
  34. node,
  35. message: `\`rel="preconnect"\` is missing from Google Font. See: ${url}`,
  36. })
  37. }
  38. },
  39. }
  40. },
  41. }