Skip to content

Commit 7c6f962

Browse files
committed
fix: broken google font mirror
1 parent 39c8ede commit 7c6f962

File tree

2 files changed

+25
-12
lines changed

2 files changed

+25
-12
lines changed

src/module.ts

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -317,12 +317,18 @@ export default defineNuxtModule<ModuleOptions>({
317317
logger.warn(`The ${fontKey} font was skipped because remote fonts are not available in StackBlitz, please use a local font.`)
318318
return false
319319
}
320-
if (await downloadFont(f, fontStorage, config.googleFontMirror)) {
320+
const result = await downloadFont(f, fontStorage, config.googleFontMirror)
321+
if (result.success) {
321322
// move file to serverFontsDir
322323
f.key = `nuxt-og-image:fonts:${fontFileBase}.ttf.base64`
323324
}
324325
else {
325-
logger.warn(`Failed to download font ${fontKey}. You may be offline or behind a firewall blocking Google. Consider setting \`googleFontMirror: true\`.`)
326+
const mirrorMsg = config.googleFontMirror
327+
? `using mirror host \`${result.host}\``
328+
: 'Consider setting `googleFontMirror: true` if you are in China or behind a firewall.'
329+
logger.warn(`Failed to download font ${fontKey} ${mirrorMsg}`)
330+
if (result.error)
331+
logger.warn(` Error: ${result.error.message || result.error}`)
326332
return false
327333
}
328334
}

src/util.ts

Lines changed: 17 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -30,31 +30,38 @@ export async function downloadFont(font: ResolvedFontConfig, storage: Storage, m
3030
const { name, weight, style } = font
3131
const key = `${name}-${style}-${weight}.ttf.base64`
3232
if (await storage.hasItem(key))
33-
return true
33+
return { success: true }
3434

3535
const host = typeof mirror === 'undefined' ? 'fonts.googleapis.com' : mirror === true ? 'fonts.font.im' : mirror
36+
const cssUrl = `https://${host}/css2?family=${name}:${style === 'ital' ? `ital,wght@1,${weight}` : `wght@${weight}`}`
37+
3638
// using H3Event $fetch will cause the request headers not to be sent
37-
const css = await $fetch(`https://${host}/css2?family=${name}:${style === 'ital' ? `ital,wght@1,${weight}` : `wght@${weight}`}`, {
39+
const css = await $fetch(cssUrl, {
3840
timeout: 10 * 1000, // 10 second timeout
3941
headers: {
4042
// Make sure it returns TTF.
4143
'User-Agent':
4244
'Mozilla/5.0 (Macintosh; U; Intel Mac OS X 10_6_8; de-at) AppleWebKit/533.21.1 (KHTML, like Gecko) Version/5.0.5 Safari/533.21.1',
4345
},
44-
}).catch(() => {
45-
return false
46+
}).catch((err) => {
47+
return { error: err }
4648
})
47-
if (!css)
48-
return false
49+
if (!css || typeof css !== 'string')
50+
return { success: false, error: css?.error || new Error(`Failed to fetch CSS from ${cssUrl}`), host }
4951

5052
const ttfResource = css.match(/src: url\((.+)\) format\('(opentype|truetype)'\)/)
5153
if (ttfResource?.[1]) {
52-
const buf = await $fetch(ttfResource[1], { baseURL: host, responseType: 'arrayBuffer' })
54+
const buf = await $fetch(ttfResource[1], { responseType: 'arrayBuffer' }).catch((err) => {
55+
return { error: err }
56+
})
57+
if (buf?.error)
58+
return { success: false, error: buf.error, host, fontUrl: ttfResource[1] }
59+
5360
// need to base 64 the buf
54-
const base64Font = Buffer.from(buf).toString('base64')
61+
const base64Font = Buffer.from(buf as ArrayBuffer).toString('base64')
5562
// output to outputPath
5663
await storage.setItem(key, base64Font)
57-
return true
64+
return { success: true }
5865
}
59-
return false
66+
return { success: false, error: new Error('No TTF resource found in CSS response'), host }
6067
}

0 commit comments

Comments
 (0)