Skip to content
Merged
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
5 changes: 5 additions & 0 deletions .changeset/fix-googleicons-glyphs.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'astro': patch
---

Fixes `fontProviders.googleicons()` returning the full icon font (~3.9MB) instead of only the requested glyphs when multiple `experimental.glyphs` are specified
21 changes: 19 additions & 2 deletions packages/astro/src/assets/fonts/providers/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -113,8 +113,25 @@ function googleicons(): FontProvider<GoogleiconsFamilyOptions | undefined> {
async init(context) {
initializedProvider = await provider(context);
},
async resolveFont({ familyName, ...rest }) {
return await initializedProvider?.resolveFont(familyName, rest);
async resolveFont({ familyName, options, ...rest }) {
// Workaround for https://github.com/unjs/unifont/issues/336
// unifont joins glyphs with .join("") instead of .join(","), producing
// an invalid `icon_names` query param that causes Google to return the
// full font. Pre-joining into a single element sidesteps the bug.
const patchedOptions =
options?.experimental?.glyphs && options.experimental.glyphs.length > 1
? {
...options,
experimental: {
...options.experimental,
glyphs: [options.experimental.glyphs.join(',')],
},
}
: options;
return await initializedProvider?.resolveFont(familyName, {
options: patchedOptions,
...rest,
});
},
async listFonts() {
return await initializedProvider?.listFonts?.();
Expand Down
80 changes: 80 additions & 0 deletions packages/astro/test/units/assets/fonts/googleicons-glyphs.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
import assert from 'node:assert/strict';
import { describe, it } from 'node:test';
import { fontProviders } from '../../../../dist/config/entrypoint.js';

describe('googleicons glyphs workaround (unifont#336)', () => {
// The googleicons provider wraps unifont and pre-joins multiple glyphs
// into a single comma-separated string before passing to unifont. This
// works around unifont's .join("") bug which concatenates icon names
// without a separator.
//
// We can't fully test the transformation without hitting the network
// (init() fetches Google's metadata), so we verify that resolveFont
// handles all option shapes without errors when the provider is
// uninitialized (returns undefined).

it('handles multiple glyphs without error', async () => {
const provider = fontProviders.googleicons();
const result = await provider.resolveFont({
familyName: 'Material Symbols Outlined',
weights: ['400'],
styles: ['normal'],
subsets: ['latin'],
formats: ['woff2'],
options: {
experimental: {
glyphs: ['add_shopping_cart', 'remove_shopping_cart', 'home'],
},
},
});
// Without init(), returns undefined — no crash means the patching logic is safe
assert.equal(result, undefined);
});

it('handles single glyph without error', async () => {
const provider = fontProviders.googleicons();
const result = await provider.resolveFont({
familyName: 'Material Symbols Outlined',
weights: ['400'],
styles: ['normal'],
subsets: ['latin'],
formats: ['woff2'],
options: {
experimental: {
glyphs: ['add_shopping_cart'],
},
},
});
assert.equal(result, undefined);
});

it('handles undefined options without error', async () => {
const provider = fontProviders.googleicons();
const result = await provider.resolveFont({
familyName: 'Material Symbols Outlined',
weights: ['400'],
styles: ['normal'],
subsets: ['latin'],
formats: ['woff2'],
options: undefined,
});
assert.equal(result, undefined);
});

it('handles empty glyphs array without error', async () => {
const provider = fontProviders.googleicons();
const result = await provider.resolveFont({
familyName: 'Material Symbols Outlined',
weights: ['400'],
styles: ['normal'],
subsets: ['latin'],
formats: ['woff2'],
options: {
experimental: {
glyphs: [],
},
},
});
assert.equal(result, undefined);
});
});
Loading