fix: iterate all weight matches in getFontPathByWeight#645
Merged
satnaing merged 2 commits intoJun 2, 2026
Conversation
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
satnaing
requested changes
Jun 2, 2026
- 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>
Contributor
Author
|
Changes applied: added |
satnaing
approved these changes
Jun 2, 2026
Owner
|
Thanks for the PR. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Description
Fixes a bug in
src/utils/getFontPathByWeight.tswhere the function fails to find a valid font path whenfontDatacontains 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 withwoff+truetype), the previous implementation usedArray.find()which stops at the first weight match — even if that entry doesn't contain the requested format. The inner.find()then returns undefined, causingCannot find the font patherrors 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 afor...ofloop that iterates all entries matching the weight and returns the URL only when both weight AND format match.Types of changes
Checklist
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...ofsolution is the smallest change that:Testing performed:
npm run buildcompletes successfully and OG images are generated correctly.Related Issue
Closes: #644