Skip to content

feat(fonts): granular preload#13826

Merged
matthewp merged 20 commits intomainfrom
feat/fonts-granular-preload
Oct 22, 2025
Merged

feat(fonts): granular preload#13826
matthewp merged 20 commits intomainfrom
feat/fonts-granular-preload

Conversation

@florian-lefebvre
Copy link
Member

@florian-lefebvre florian-lefebvre commented May 20, 2025

Changes

  • Requested on the Stage 3 RFC
  • Until now you only had the ability to preload all or nothing for a given family
  • With this PR, it's now possible to filter based on weight, style and subset

Testing

  • Added unit test for renderFontWeight
  • Updated the orchestrate unit tests to verify we collect the weight/style preload data
  • Tested manually
  • Updated the fixture test to check filtering works

Docs

@florian-lefebvre florian-lefebvre self-assigned this May 20, 2025
@changeset-bot
Copy link

changeset-bot bot commented May 20, 2025

🦋 Changeset detected

Latest commit: fe4bcce

The changes in this PR will be included in the next version bump.

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR

@github-actions github-actions bot added the pkg: astro Related to the core `astro` package (scope) label May 20, 2025
@codspeed-hq
Copy link

codspeed-hq bot commented May 20, 2025

CodSpeed Performance Report

Merging #13826 will not alter performance

Comparing feat/fonts-granular-preload (fe4bcce) with main (6cdade4)1

Summary

✅ 6 untouched

Footnotes

  1. No successful run was found on main (460810b) during the generation of this report, so 6cdade4 was used instead as the comparison base. There might be some changes unrelated to this pull request in this report.

@florian-lefebvre florian-lefebvre added the pr preview Apply this label to a PR to generate a preview release label May 20, 2025
@github-actions github-actions bot removed the pr preview Apply this label to a PR to generate a preview release label May 20, 2025
@pkg-pr-new
Copy link

pkg-pr-new bot commented May 20, 2025

astro

npm i https://pkg.pr.new/astro@13826

@astrojs/cloudflare

npm i https://pkg.pr.new/@astrojs/cloudflare@13826

@astrojs/netlify

npm i https://pkg.pr.new/@astrojs/netlify@13826

@astrojs/node

npm i https://pkg.pr.new/@astrojs/node@13826

@astrojs/vercel

npm i https://pkg.pr.new/@astrojs/vercel@13826

commit: 3fb4e63

@jonasgeiler
Copy link
Contributor

Seems to be working perfectly for me! Thanks a lot for working on this! :shipit:

@florian-lefebvre florian-lefebvre added the pr preview Apply this label to a PR to generate a preview release label Jul 14, 2025
@github-actions github-actions bot removed the pr preview Apply this label to a PR to generate a preview release label Jul 14, 2025
@JusticeMatthew
Copy link
Contributor

Testing this out now, currently I have a single font preloaded with 1 style and 1 weight

<Font cssVariable="--astro-barlow-normal" preload={[{ weight: 400, style: "normal" }]} />

However, I am still showing the following in my dev tools (the font is actually being used as well):

image image

@florian-lefebvre
Copy link
Member Author

Is the repo public? If not a minimal repro would help, it's basically impossible to guess just from this

@JusticeMatthew
Copy link
Contributor

@florian-lefebvre
Copy link
Member Author

I cloned the repo locally and I don't get any warnings in the console (tested on Brave and Firefox)

@JusticeMatthew
Copy link
Contributor

Hmm wonder what the discrepancy is? I'm also using firefox and experienced the exact same thing I posted images of (3 unused preload warnings and 3 network requests) 🤔

Even in incognito I still get it

@hkbertoson
Copy link

I just tested this in Firefox and I get the errors.
CleanShot 2025-07-14 at 14 37 40

And it also seems to be using the 700 weight when its set to use the 400.
image

I did some more digging and I think this may just be a bug for preloading fonts in Firefox itself.

https://bugzilla.mozilla.org/buglist.cgi?quicksearch=Preload+Fonts

@florian-lefebvre
Copy link
Member Author

Issue was that by default several subsets are loaded. Adding subsets: ['latin'] solved it!

@JusticeMatthew
Copy link
Contributor

And to further clarify the font-weight was wrong because I didn't apply one via the CSS in my minimal repro

That is also working correctly now too 👍

Awsome work @florian-lefebvre !

@jyasskin
Copy link
Contributor

It would be nice to support preloading certain subsets, but still making the other subsets available in case I use a rare character in one page. My brief testing seemed to show the subsets property emitting just those @font-face rules, in addition to only emitting those preloads.

@florian-lefebvre
Copy link
Member Author

Good idea! Unfortunately, we currently do not have access to the subset for each link but that may become possible if unjs/unifont#242 is approved

@florian-lefebvre florian-lefebvre marked this pull request as ready for review October 16, 2025 13:24
Copy link
Contributor

@ascorbic ascorbic left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It would be good to explain in the changeset the behaviour with variable fonts


function checkWeight(input: string, target: string): boolean {
// If the input looks like "100 900", we check it as is
if (input.includes(' ')) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd trim both before all of these, because a stray leading or trailing space could mess up the logic

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah good idea! I won't trim the target tho because it comes from the provider, so it's its responsability to handle variable weights correctly

Copy link
Contributor

@ascorbic ascorbic left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Great!

Copy link
Member

@sarah11918 sarah11918 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Quick comments from me, but I've also reviewed the docs PR which will inform the changeset here, I think!

florian-lefebvre and others added 2 commits October 17, 2025 14:29
Co-authored-by: Sarah Rainsberger <5098874+sarah11918@users.noreply.github.com>
Copy link
Member

@sarah11918 sarah11918 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just caught an extra code fence in the code block, but otherwise changeset LGTM! 🎉

Co-authored-by: Sarah Rainsberger <5098874+sarah11918@users.noreply.github.com>
@matthewp matthewp merged commit 8079482 into main Oct 22, 2025
22 checks passed
@matthewp matthewp deleted the feat/fonts-granular-preload branch October 22, 2025 13:39
@astrobot-houston astrobot-houston mentioned this pull request Oct 22, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

pkg: astro Related to the core `astro` package (scope)

Projects

None yet

Development

Successfully merging this pull request may close these issues.

8 participants