forked from vercel/next.js
-
Notifications
You must be signed in to change notification settings - Fork 0
/
google-font-preconnect.js
46 lines (41 loc) · 1.15 KB
/
google-font-preconnect.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
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}`,
})
}
},
}
},
}