-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathgatsby-browser.js
41 lines (35 loc) · 1001 Bytes
/
gatsby-browser.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
import React from "react";
import { AsyncFonts } from "./components";
import { MODE_DEFAULT, SCOPE_DEFAULT } from "./consts";
import { getFontFiles, getFontNames } from "./utils";
import { fontListener } from "./utils/fontListener";
export const onClientEntry = (
_,
{ custom = [], web = [], enableListener = false, scope = SCOPE_DEFAULT }
) => {
if (!enableListener) {
return;
}
const allFonts = [...custom, ...web];
const fontNames = getFontNames(allFonts);
const listenerProps = { fontNames, scope };
fontListener(listenerProps);
};
export const wrapRootElement = (
{ element },
{ custom = [], web = [], mode = MODE_DEFAULT }
) => {
if (mode !== "async") {
return element;
}
const allFonts = [...custom, ...web];
const fontFiles = getFontFiles(allFonts);
const fontNames = getFontNames(allFonts);
const hasFontNames = Boolean(fontNames.length);
return (
<>
{hasFontNames && <AsyncFonts hrefs={fontFiles} />}
{element}
</>
);
};