forked from vercel/next.js
-
Notifications
You must be signed in to change notification settings - Fork 0
/
google-font-display.js
61 lines (53 loc) · 1.58 KB
/
google-font-display.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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
const NodeAttributes = require('../utils/node-attributes.js')
const url = 'https://nextjs.org/docs/messages/google-font-display'
module.exports = {
meta: {
docs: {
description: 'Enforce font-display behavior with Google Fonts.',
recommended: true,
url,
},
type: 'problem',
schema: [],
},
create: function (context) {
return {
JSXOpeningElement(node) {
let message
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 isGoogleFont =
typeof hrefValue === 'string' &&
hrefValue.startsWith('https://fonts.googleapis.com/css')
if (isGoogleFont) {
const params = new URLSearchParams(hrefValue.split('?')[1])
const displayValue = params.get('display')
if (!params.has('display')) {
message =
'A font-display parameter is missing (adding `&display=optional` is recommended).'
} else if (
displayValue === 'auto' ||
displayValue === 'block' ||
displayValue === 'fallback'
) {
message = `${
displayValue[0].toUpperCase() + displayValue.slice(1)
} is not recommended.`
}
}
if (message) {
context.report({
node,
message: `${message} See: ${url}`,
})
}
},
}
},
}