From 0a7641233bfaf3ce165942ec9dddd3e81cc29b93 Mon Sep 17 00:00:00 2001 From: astrobot-houston Date: Fri, 31 Jul 2026 23:45:40 +0000 Subject: [PATCH] Fix googleicons provider returning full font when multiple glyphs are specified (#17565) Work around unifont#336 where .join("") concatenates icon names without a separator, producing an invalid `icon_names` query param. Pre-join multiple glyphs into a single comma-separated element so unifont's .join("") produces the correct value for Google's API. --- .changeset/fix-googleicons-glyphs.md | 5 ++ .../astro/src/assets/fonts/providers/index.ts | 21 ++++- .../assets/fonts/googleicons-glyphs.test.ts | 80 +++++++++++++++++++ 3 files changed, 104 insertions(+), 2 deletions(-) create mode 100644 .changeset/fix-googleicons-glyphs.md create mode 100644 packages/astro/test/units/assets/fonts/googleicons-glyphs.test.ts diff --git a/.changeset/fix-googleicons-glyphs.md b/.changeset/fix-googleicons-glyphs.md new file mode 100644 index 000000000000..b2773cd05a43 --- /dev/null +++ b/.changeset/fix-googleicons-glyphs.md @@ -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 diff --git a/packages/astro/src/assets/fonts/providers/index.ts b/packages/astro/src/assets/fonts/providers/index.ts index d113604a3e53..3c228d41b575 100644 --- a/packages/astro/src/assets/fonts/providers/index.ts +++ b/packages/astro/src/assets/fonts/providers/index.ts @@ -113,8 +113,25 @@ function googleicons(): FontProvider { 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?.(); diff --git a/packages/astro/test/units/assets/fonts/googleicons-glyphs.test.ts b/packages/astro/test/units/assets/fonts/googleicons-glyphs.test.ts new file mode 100644 index 000000000000..a544f6de1cb8 --- /dev/null +++ b/packages/astro/test/units/assets/fonts/googleicons-glyphs.test.ts @@ -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); + }); +});