({
functionName,
data,
emitFontFile,
isDev,
isServer,
})
| 26 | } |
| 27 | |
| 28 | const nextFontGoogleFontLoader: FontLoader = async ({ |
| 29 | functionName, |
| 30 | data, |
| 31 | emitFontFile, |
| 32 | isDev, |
| 33 | isServer, |
| 34 | }) => { |
| 35 | const { |
| 36 | fontFamily, |
| 37 | weights, |
| 38 | styles, |
| 39 | display, |
| 40 | preload, |
| 41 | selectedVariableAxes, |
| 42 | fallback, |
| 43 | adjustFontFallback, |
| 44 | variable, |
| 45 | subsets, |
| 46 | } = validateGoogleFontFunctionCall(functionName, data[0]) |
| 47 | |
| 48 | // Validate and get the font axes required to generated the URL |
| 49 | const fontAxes = getFontAxes( |
| 50 | fontFamily, |
| 51 | weights, |
| 52 | styles, |
| 53 | selectedVariableAxes |
| 54 | ) |
| 55 | |
| 56 | // Generate the Google Fonts URL from the font family, axes and display value |
| 57 | const url = getGoogleFontsUrl(fontFamily, fontAxes, display) |
| 58 | |
| 59 | // Get precalculated fallback font metrics, used to generate the fallback font CSS |
| 60 | const adjustFontFallbackMetrics: AdjustFontFallback | undefined = |
| 61 | adjustFontFallback ? getFallbackFontOverrideMetrics(fontFamily) : undefined |
| 62 | |
| 63 | const result = { |
| 64 | fallbackFonts: fallback, |
| 65 | weight: |
| 66 | weights.length === 1 && weights[0] !== 'variable' |
| 67 | ? weights[0] |
| 68 | : undefined, |
| 69 | style: styles.length === 1 ? styles[0] : undefined, |
| 70 | variable, |
| 71 | adjustFontFallback: adjustFontFallbackMetrics, |
| 72 | } |
| 73 | |
| 74 | try { |
| 75 | /** |
| 76 | * Hacky way to make sure the fetch is only done once. |
| 77 | * Otherwise both the client and server compiler would fetch the CSS. |
| 78 | * The reason we need to return the actual CSS from both the server and client is because a hash is generated based on the CSS content. |
| 79 | */ |
| 80 | const hasCachedCSS = cssCache.has(url) |
| 81 | // Fetch CSS from Google Fonts or get it from the cache |
| 82 | let fontFaceDeclarations = hasCachedCSS |
| 83 | ? cssCache.get(url) |
| 84 | : await fetchCSSFromGoogleFonts(url, fontFamily, isDev).catch((err) => { |
| 85 | console.error(err) |
no test coverage detected
searching dependent graphs…