-
Notifications
You must be signed in to change notification settings - Fork 7
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Question: How to fetch only woff2 format? #195
Comments
Hey @lttr! I'm here to assist you with any bugs, questions, or contributions you have. Let's tackle this font format issue together! To configure the Fonts module to download only the export default {
fonts: {
families: [
{
name: 'Poppins',
provider: 'google',
src: 'url(https://example.com/font.woff2)'
}
]
}
} This configuration ensures that only the import type { FontProvider } from '@nuxt/fonts'
const resolvableFonts = new Set<string>()
export default {
async setup() {
// Do some stuff
resolvableFonts.add('Poppins')
},
async resolveFontFaces(fontFamily) {
if (!resolvableFonts.has(fontFamily)) {
return
}
return {
fonts: {
src: 'url(https://example.com/font.woff2)',
},
}
},
} satisfies FontProvider This custom provider setup ensures that only the |
While the bot might be right with the answer above, it misses the point. If I want I can use the URL directly or I might download the fonts manually. The advantage here is that this module could do it automatically. |
It does not work since I need 2 different subsets as mentioned in a code snippet in the original description. @dosu |
To configure the Fonts module to download only the Here is the relevant code snippet with the necessary modifications: const userAgents = {
woff2: 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/121.0.0.0 Safari/537.36',
}
async function getFontDetails(family: string, variants: ResolveFontFacesOptions) {
const font = fonts.find(font => font.family === family)!
const styles = [...new Set(variants.styles.map(i => styleMap[i]))].sort()
const variableWeight = font.axes.find(a => a.tag === 'wght')
const weights = variableWeight
? [`${variableWeight.min}..${variableWeight.max}`]
: variants.weights.filter(weight => weight in font.fonts)
if (weights.length === 0 || styles.length === 0) return []
const resolvedVariants = weights.flatMap(w => [...styles].map(s => `${s},${w}`)).sort()
let css = ''
for (const extension in userAgents) {
css += await $fetch<string>('/css2', {
baseURL: 'https://fonts.googleapis.com',
headers: { 'user-agent': userAgents[extension as keyof typeof userAgents] },
query: {
family: family + ':' + 'ital,wght@' + resolvedVariants.join(';'),
subset: variants.subsets.join(','), // Ensure subsets are included in the query
},
})
}
return addLocalFallbacks(family, extractFontFaceData(css))
} In this code:
Additionally, you can configure the Fonts module options in your Nuxt configuration to specify the Google provider and other relevant settings: export default {
fonts: {
providers: {
google: true,
},
defaults: {
subsets: ['latin', 'latin-ext'], // Specify the subsets you need
},
},
} This configuration ensures that the Fonts module uses the Google provider and includes the specified subsets by default [1][2]. |
@lttr In this case, the browser seems to be downloading woff. |
@neobutter Can you elaborate on this? I don't really understand how to workaround this. I'm not interested in woff at all, browsers support woff2 just fine. I would even welcome a switch in the nuxt/fonts module to completely disable all formats other than woff2. |
let's track in #204. |
Since woff2 format is often enough, I would like to have woff2 and only woff2 format of a font downloaded. I have not found a way how to configure a provider like Google in order to force only this format.
Details
I have tried the Fonts module both without any configuration and with configuration like this one:
In both cases the behavior is following:
I was unable to create a reproduction on Stackblitz since fonts are not at all downloaded there with Google provider.
The text was updated successfully, but these errors were encountered: