fonts icon indicating copy to clipboard operation
fonts copied to clipboard

fix: terminate the build process when font resolution fails

Open cernymatej opened this issue 9 months ago โ€ข 5 comments

๐Ÿ”— Linked issue

resolves #356

โ“ Type of change

  • [ ] ๐Ÿ“– Documentation (updates to the documentation or readme)
  • [ ] ๐Ÿž Bug fix (a non-breaking change that fixes an issue)
  • [x] ๐Ÿ‘Œ Enhancement (improving an existing functionality)
  • [ ] โœจ New feature (a non-breaking change that adds functionality)
  • [ ] ๐Ÿงน Chore (updates to the build process or auxiliary tools and libraries)
  • [ ] โš ๏ธ Breaking change (fix or feature that would cause existing functionality to change)

๐Ÿ“š Description

This will ensure that the build process is terminated when the font face declarations cannot be produced, preventing a project with broken fonts from being deployed.


I'm wondering if this should be addressed upstream in unifont as well. https://github.com/unjs/unifont/blob/f4d3efae764b78ab4c11cf303e465ecf5b95c1a4/src/index.ts#L55-L57 https://github.com/unjs/unifont/blob/f4d3efae764b78ab4c11cf303e465ecf5b95c1a4/src/index.ts#L79-L81

However, that would likely require a major version bump since it would be a breaking change. Then again, it might not be necessary, as one could simply check the return value to determine whether the resolution was successful.

cernymatej avatar Feb 21 '25 11:02 cernymatej

Codecov Report

All modified and coverable lines are covered by tests :white_check_mark:

Project coverage is 82.73%. Comparing base (33e98b0) to head (b81a658).

Additional details and impacted files
@@            Coverage Diff             @@
##             main     #518      +/-   ##
==========================================
- Coverage   82.84%   82.73%   -0.11%     
==========================================
  Files          13       13              
  Lines        1067     1066       -1     
  Branches      264      268       +4     
==========================================
- Hits          884      882       -2     
- Misses        178      179       +1     
  Partials        5        5              

:umbrella: View full report in Codecov by Sentry.
:loudspeaker: Have feedback on the report? Share it here.

codecov-commenter avatar Feb 21 '25 11:02 codecov-commenter

It's possible that nuxt/fonts might process something that isn't a valid font (and therefore can't resolve it).

So I think the scenarios we should care about are:

  • if the font providers can't initialise their registries
  • if the font providers don't provide font data even though they have the font in their registry
  • if we can't download the fonts

danielroe avatar Feb 21 '25 12:02 danielroe

Could you please share an example of a case where something fails to resolve, but we wouldnโ€™t want to throw an error here? ๐Ÿ™ I'd like to test it.

The resolveFont function from unifont receives a fontFamily parameter, so I assumed it was already considered a valid font face.
https://github.com/nuxt/fonts/blob/6632fa2f2378af20d94ab87b37979708c4adc5ee/src/module.ts#L211


Are you suggesting that we should throw an error when a failure occurs in these places instead?

Downloading registries https://github.com/unjs/unifont/blob/f4d3efae764b78ab4c11cf303e465ecf5b95c1a4/src/index.ts#L51

Downloading font details https://github.com/unjs/unifont/blob/f4d3efae764b78ab4c11cf303e465ecf5b95c1a4/src/index.ts#L71

Downloading fonts https://github.com/nuxt/fonts/blob/6632fa2f2378af20d94ab87b37979708c4adc5ee/src/assets.ts#L153 (probably unnecessary, since errors arenโ€™t caught here anyway - we can keep the fetch error, if it occurs)

cernymatej avatar Feb 21 '25 14:02 cernymatej

It might fail - I think - if the font family isn't a web font or if it's processing a css variable which doesn't have a font family.

danielroe avatar Mar 08 '25 21:03 danielroe

we can use https://github.com/unjs/unifont/pull/161 once the next release of unifont

danielroe avatar Apr 21 '25 10:04 danielroe