MCPcopy
hub / github.com/vercel/next.js / nextFontGoogleFontLoader

Function nextFontGoogleFontLoader

packages/font/src/google/loader.ts:28–194  ·  view source on GitHub ↗
({
  functionName,
  data,
  emitFontFile,
  isDev,
  isServer,
})

Source from the content-addressed store, hash-verified

26}
27
28const 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)

Callers 1

loader.test.tsFile · 0.85

Calls 15

getFontAxesFunction · 0.90
getGoogleFontsUrlFunction · 0.90
fetchCSSFromGoogleFontsFunction · 0.90
nextFontErrorFunction · 0.90
findFontFilesInCssFunction · 0.90
fetchFontFileFunction · 0.90
splitMethod · 0.80
allMethod · 0.80
escapeStringRegexpFunction · 0.70
getMethod · 0.65

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…