Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 18 additions & 1 deletion src/providers/google.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,12 @@ interface ProviderOption {
glyphs?: {
[fontFamily: string]: string[]
}
/**
* Experimental: include only modern font format without fallback for old browsers
*/
modernFormatsOnly?: boolean | {
[fontFamily: string]: boolean
}
}
}

Expand Down Expand Up @@ -113,7 +119,18 @@ export default defineFontProvider<ProviderOption>('google', async (_options = {}
let priority = 0
const resolvedFontFaceData: FontFaceData[] = []

for (const extension in userAgents) {
let formats = Object.keys(userAgents)
const modernFormatsOnly = _options.experimental?.modernFormatsOnly
if (
modernFormatsOnly === true
|| (modernFormatsOnly
&& typeof modernFormatsOnly === 'object'
&& modernFormatsOnly?.[family] === true)
) {
formats = formats.slice(0, 1) // keep only 1st user agent
}

for (const extension of formats) {
const rawCss = await $fetch<string>('/css2', {
baseURL: 'https://fonts.googleapis.com',
headers: {
Expand Down
28 changes: 28 additions & 0 deletions test/providers/google.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -215,4 +215,32 @@ body {
})
expect(fonts.length).toBe(18)
})

it('experimental.modernFormatsOnly', async () => {
const unifont = await createUnifont([
providers.google({
experimental: {
modernFormatsOnly: {
Poppins: true,
},
},
}),
])
{
const { fonts } = await unifont.resolveFont('Poppins', {})
const remoteFontSources = fonts.flatMap(fnt =>
fnt.src.flatMap(src => ('url' in src ? src : [])),
)
expect(remoteFontSources).not.toEqual([])
expect(remoteFontSources.filter(s => s.format !== 'woff2')).toEqual([])
}
{
const { fonts } = await unifont.resolveFont('Geist', {})
const remoteFontSources = fonts.flatMap(fnt =>
fnt.src.flatMap(src => ('url' in src ? src : [])),
)
expect(remoteFontSources).not.toEqual([])
expect(remoteFontSources.filter(s => s.format !== 'woff2')).not.toEqual([])
}
})
})