question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

[Font Optimization] Asynchronous non-critical font styles

See original GitHub issue

Feature request

Whilst I hugely appreciate @prateekbh’s work to enhance the external font loading experience, it would be nice to support (or maybe even make default) the option to treat these font styles as non-critical.

RE: https://github.com/vercel/next.js/pull/14746, https://github.com/vercel/next.js/pull/16031

Describe the solution you’d like

An async option could be defined in the experimental config (but I would argue it might be best to assume that font styles are non-critical and set this to true by default) which would extract the external CSS into a separate CSS file.

This CSS file would be referenced in the head, and set to media="print" on initial load then to media="all" on app hydration. With lots of modern CSS-in-JS libraries extracting to critical CSS, this would avoid clashing opinions on what should actually by considered crticial.

Describe alternatives you’ve considered

I built and maintain next-google-fonts but this doesn’t extract CSS at compile time; it just asynchronously loads Google Fonts.

Additional context

Again, thanks so much for your work on this @prateekbh. Happy to help out where I can

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Reactions:5
  • Comments:9 (8 by maintainers)

github_iconTop GitHub Comments

5reactions
prateekbhcommented, Aug 14, 2020

Well choosing display=swap makes your text appear faster but definitely has a problem of FOUT. I will soon look into the effects of display:optional + adding a preload with this optimization. This option adds a 100ms wait to FCP and can render the text in actual font without FOUT.

I would suggest people to opt for the async css when they are very sure that fonts are of least interest but not something to come out of the box, given that if you’re adding a font in the first place you might want it to load faster as well than later.

1reaction
joe-bellcommented, Dec 1, 2020

Hey @willemmulder, I think this might be useful to add over at #12984

Read more comments on GitHub >

github_iconTop Results From Across the Web

How to Load Fonts in a Way That Fights FOUT and Makes ...
Clear the styles for the fallback font once the asynchronously loaded font file has has rendered, and apply the styles intended for the...
Read more >
Modern optimization strategies for loading web fonts
1. Self host the web fonts · 2. Use variable fonts · 3. Use appropriate font-display value · 4. Preload web fonts ·...
Read more >
Basic Features: Font Optimization - Next.js
Optimizing loading web fonts with the built-in `@next/font` loaders. ... You can specify multiple weights and/or styles by using an array:
Read more >
Defer non-critical CSS - web.dev
The goal of this guide is to optimize this page, so only the critical styles are loaded synchronously, while the rest (like the...
Read more >
The Fastest Google Fonts - CSS Wizardry
Async CSS: Loading the Google Fonts File asynchronously. ... I wouldn't be able to do this bit without her Font Style Matcher. Snippet:....
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found