google-font-display.js 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. const NodeAttributes = require('../utils/node-attributes.js')
  2. const url = 'https://nextjs.org/docs/messages/google-font-display'
  3. module.exports = {
  4. meta: {
  5. docs: {
  6. description: 'Enforce font-display behavior 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. let message
  17. if (node.name.name !== 'link') {
  18. return
  19. }
  20. const attributes = new NodeAttributes(node)
  21. if (!attributes.has('href') || !attributes.hasValue('href')) {
  22. return
  23. }
  24. const hrefValue = attributes.value('href')
  25. const isGoogleFont =
  26. typeof hrefValue === 'string' &&
  27. hrefValue.startsWith('https://fonts.googleapis.com/css')
  28. if (isGoogleFont) {
  29. const params = new URLSearchParams(hrefValue.split('?')[1])
  30. const displayValue = params.get('display')
  31. if (!params.has('display')) {
  32. message =
  33. 'A font-display parameter is missing (adding `&display=optional` is recommended).'
  34. } else if (
  35. displayValue === 'auto' ||
  36. displayValue === 'block' ||
  37. displayValue === 'fallback'
  38. ) {
  39. message = `${
  40. displayValue[0].toUpperCase() + displayValue.slice(1)
  41. } is not recommended.`
  42. }
  43. }
  44. if (message) {
  45. context.report({
  46. node,
  47. message: `${message} See: ${url}`,
  48. })
  49. }
  50. },
  51. }
  52. },
  53. }