forked from codeAdrian/gatsby-omni-font-loader
-
Notifications
You must be signed in to change notification settings - Fork 0
/
getFontConfig.tsx
56 lines (49 loc) · 1.45 KB
/
getFontConfig.tsx
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
import React from "react"
import { arrayCheck, kebabCase } from "../utils"
export const getFontConfig = (
preconnectConfig: string[],
preloadConfig: string[],
renderBlockingFonts?: { name: string | string[]; file: string }[]
) => {
const headComponents = []
if (arrayCheck(preconnectConfig)) {
preconnectConfig.forEach(href => {
headComponents.push(
<link
key={`preconnect-${href}`}
rel="preconnect"
href={href}
crossOrigin="true"
/>
)
})
}
// if (arrayCheck(preloadConfig)) {
// preloadConfig.forEach(href => {
// headComponents.push(
// <link key={`preload-${href}`} rel="stylesheet" href={href} />
// )
// })
// }
// // APPROACH I
// // According to https://pagespeedchecklist.com/asynchronous-google-fonts
// // Uncomment these lines to "optionally increase loading priority"
// if (arrayCheck(preloadConfig)) {
// preloadConfig.forEach((href) => {
// headComponents.push(
// <link key={`preload-${href}`} rel="preload" as="style" href={href} />
// );
// });
// }
if (arrayCheck(renderBlockingFonts)) {
renderBlockingFonts.forEach(({ name, file }) => {
const key = Array.isArray(name)
? name.map(n => kebabCase(n)).join("-")
: kebabCase(name)
headComponents.push(
<link key={`render-blocking-${key}`} href={file} rel="stylesheet" />
)
})
}
return headComponents
}