12345678910111213141516171819202122232425262728293031323334353637383940414243444546 |
- const NodeAttributes = require('../utils/node-attributes.js')
- const url = 'https://nextjs.org/docs/messages/google-font-preconnect'
- module.exports = {
- meta: {
- docs: {
- description: 'Ensure `preconnect` is used with Google Fonts.',
- recommended: true,
- url,
- },
- type: 'problem',
- schema: [],
- },
- create: function (context) {
- return {
- JSXOpeningElement(node) {
- if (node.name.name !== 'link') {
- return
- }
- const attributes = new NodeAttributes(node)
- if (!attributes.has('href') || !attributes.hasValue('href')) {
- return
- }
- const hrefValue = attributes.value('href')
- const preconnectMissing =
- !attributes.has('rel') ||
- !attributes.hasValue('rel') ||
- attributes.value('rel') !== 'preconnect'
- if (
- typeof hrefValue === 'string' &&
- hrefValue.startsWith('https://fonts.gstatic.com') &&
- preconnectMissing
- ) {
- context.report({
- node,
- message: `\`rel="preconnect"\` is missing from Google Font. See: ${url}`,
- })
- }
- },
- }
- },
- }
|