Skip to content

fix: iterate all weight matches in getFontPathByWeight#645

Merged
satnaing merged 2 commits into
satnaing:mainfrom
cfresco-fb:fix/font-path-multiple-entries
Jun 2, 2026
Merged

fix: iterate all weight matches in getFontPathByWeight#645
satnaing merged 2 commits into
satnaing:mainfrom
cfresco-fb:fix/font-path-multiple-entries

Conversation

@cfresco-fb

Copy link
Copy Markdown
Contributor

Description

Fixes a bug in src/utils/getFontPathByWeight.ts where the function fails to find a valid font path when fontData contains multiple entries per weight with different format sets.

When the font provider returns multiple entries for the same weight (e.g., one with only woff2, another with woff + truetype), the previous implementation used Array.find() which stops at the first weight match — even if that entry doesn't contain the requested format. The inner .find() then returns undefined, causing Cannot find the font path errors during OG image generation.

This happens when changing the default font from "Google Sans Code" to certain other Google Fonts (e.g., Inter).

The fix replaces Array.find() with a for...of loop that iterates all entries matching the weight and returns the URL only when both weight AND format match.

Types of changes

  • Bug Fix (non-breaking change which fixes an issue)
  • New Feature (non-breaking change which adds functionality)
  • Documentation Update (if none of the other choices apply)
  • Others (any other types not listed above)

Checklist

  • I have read the Contributing Guide
  • I have added the necessary documentation (if appropriate)
  • Breaking Change (fix or feature that would cause existing functionality to not work as expected)

Further comments

This is a surface-level fix in getFontPathByWeight. The root behavior of the font provider returning multiple entries per weight comes from the Astro fonts API and is outside the scope of this PR.

I considered other approaches such as deduplicating entries upstream or filtering by format before by weight, but the for...of solution is the smallest change that:

  • Preserves the existing function signature.
  • Doesn't introduce any new dependencies or refactor unrelated code.
  • Behaves identically to the original implementation when there is only one entry per weight (no regression).

Testing performed:

  • Reproduced the bug locally by changing the font from "Google Sans Code" to "Inter".
  • Confirmed the fix resolves the issue: npm run build completes successfully and OG images are generated correctly.
  • Verified no regression with the default font configuration.

Related Issue

Closes: #644

When fontData has multiple entries for the same weight with different format sets, Array.find() stops at the first match even if that entry does not contain the requested format. The fix uses a for...of loop that returns only when both weight AND format match. Fixes satnaing#644
Comment thread src/utils/getFontPathByWeight.ts Outdated
Comment thread src/utils/getFontPathByWeight.ts Outdated
- Use `?? font.src[0]` as fallback when no src entry matches the
  requested format, as suggested in code review
- Add missing trailing newline at end of file

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
@cfresco-fb

Copy link
Copy Markdown
Contributor Author

Changes applied: added ?? font.src[0] fallback and fixed the missing trailing newline. Ready for re-review.

@satnaing

satnaing commented Jun 2, 2026

Copy link
Copy Markdown
Owner

Thanks for the PR.

@satnaing
satnaing merged commit e062c79 into satnaing:main Jun 2, 2026
1 check passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[BUG]: getFontPathByWeight fails when font has multiple entries per weight with different formats

2 participants