Skip to content

Commit

Permalink
perf(web-fonts): improve web fonts loading
Browse files Browse the repository at this point in the history
  • Loading branch information
antfu committed Aug 4, 2022
1 parent d41bd81 commit ae64288
Showing 1 changed file with 10 additions and 12 deletions.
22 changes: 10 additions & 12 deletions packages/preset-web-fonts/src/index.ts
Expand Up @@ -41,23 +41,21 @@ const preset = (options: WebFontsOptions = {}): Preset<any> => {
)
const fonts = Object.values(fontObject).flatMap(i => i)

const importCache: Record<string, string> = {}
const importCache: Record<string, Promise<string>> = {}

async function importUrl(url: string) {
if (inlineImports) {
if (!importCache[url]) {
try {
const { $fetch } = await import('ohmyfetch')
importCache[url] = await $fetch(url, { headers: {} })
}
catch (e) {
console.error('Failed to fetch web fonts')
console.error(e)
if (typeof process !== 'undefined' && process.env.CI)
throw e
}
const { $fetch } = await import('ohmyfetch')
importCache[url] = $fetch(url, { headers: {}, retry: 3 })
.catch((e) => {
console.error('Failed to fetch web fonts')
console.error(e)
if (typeof process !== 'undefined' && process.env.CI)
throw e
})
}
return importCache[url]
return await importCache[url]
}
else {
return `@import url('${url}')`
Expand Down

0 comments on commit ae64288

Please sign in to comment.