Skip to content
Discussion options

You must be logged in to vote

Final util seems to be working now?

const API_KEY = process.env.GOOGLE_FONTS_API_KEY

const CACHE = new Map<string, ArrayBuffer>()

export async function fetchGoogleFont({
  family,
  weight = 400,
}: {
  family: string
  weight?: number
}) {
  const fontFamily = family.replace(/ /g, '+')
  const key = `${fontFamily}:${weight}`
  const cached = CACHE.get(key)

  if (cached) {
    return cached
  }

  const variant = weight === 400 ? 'regular' : `${weight}`

  const url = `https://www.googleapis.com/webfonts/v1/webfonts?key=${API_KEY}&family=${fontFamily}`
  const res = await fetch(url)

  if (!res.ok) {
    throw new Error('Failed to fetch Google Fonts metadata')
  }

  const json = await r…

Replies: 1 comment

Comment options

You must be logged in to vote
0 replies
Answer selected by lancejpollard
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Help
Labels
None yet
1 participant