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.

suggest font-display optional if font is preloaded, to reduce layout shift

See original GitHub issue

Starting in Chrome 83, preloaded fonts with font-display: optional will result in no FOIT or layout shifts. See https://web.dev/preload-optional-fonts/

Lighthouse has two related audits on this:

uses-rel-preload recommends requests that should be, but currently aren’t, preloaded. This is based on a Lantern simulation.

font-display simply points out fonts that don’t define a font-display rule. Our docs suggest they do swap, but any value passes this audit.

To keep this audit simple, I suggest we only look at already preloaded fonts. If a preloaded font is not marked optional, the audit suggests making it so to avoid shifts.

The alternative would be: basically duplicate/reuse uses-rel-preload with a focus on fonts and suggest both preloading AND optional in one go. I think this advice would be too broad, and is certainly more complex. Let’s just encourage usage of optional if the developer has already decided to preload a font.

That’s my pitch. Open to discussion.

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Reactions:3
  • Comments:28 (6 by maintainers)

github_iconTop GitHub Comments

4reactions
xiaochenghcommented, Aug 12, 2020

Hi, I’m the one who implemented the change, so feel free to ping me if you want to know about the technical details.

I think it’s good idea to suggest preloading for optional fonts. Without preloading, the font will pretty much not be used unless you just refreshed the page (so that you have a warm memory cache). Besides:

  • Using document.fonts.load() probably should count as a preload
  • We just landed a change into M86 to increase the chance that an optional font gets used, while still ensuring no layout shift. Its main purpose is to reduce breakage, so it’s not an alternative of preloading, and developers shouldn’t rely too much on it.

And I’m not sure we should suggest setting font-display: optional for preloaded fonts, regardless of icon font or not. Setting optional means the font won’t be used when the user has a cold cache and slow network. So we should never set optional on a font that’s critical to the page. This includes icon font and some other cases.

Alternative thoughts:

  • Maybe we can suggest optional for non-critical fonts. Not sure how to identify non-critical fonts, though
  • Maybe we can suggest optional with a warning that this comes at a cost that the font may not be used, and hope it won’t be misused
3reactions
xiaochenghcommented, Aug 15, 2020

A note: The challenges of preloading with <link rel=preload> can be avoided with document.fonts.load(). We just need a font value that contains the font family name, weight and style. No need to know the actual URL.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Prevent layout shifting and flashes of invisible text (FOIT) by ...
By optimizing rendering cycles, Chrome 83 eliminates layout shifting when preloading optional fonts. Combining with font-display: optional ...
Read more >
How to avoid layout shifts caused by web fonts - Simon Hearne
Reduce layout shift with f-mods #. If font-display: optional is not possible for your design, we need to attempt to match the fallback...
Read more >
How To Fix Cumulative Layout Shift (CLS) Issues
To avoid font-induced layout shifts altogether, we could of course not use web fonts at all — including using system fonts instead, or...
Read more >
preload with font-display: optional is an Anti-pattern
It's a perfectly acceptable method if web fonts are lower on the priority list for your use case's requirements. For the record, I'm...
Read more >
How to Reduce Layout Reflow When Using Web Fonts
Cumulative Layout Shift (CLS) is a metric for web page quality that quantifies this experience. When a web font is loaded a little...
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