Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

CSS Font-display:swap blocking

Tags:

css

gtmetrix

I'm trying to optimize my website download speed by preventing Google font downloads from blocking the rest of the page from loading. Maybe I've misunderstood the font-display:swap feature of CSS, as I thought this was supposed to allow the font to download, without blocking continuing downloads.

However, the font-display tag in my CSS doesn't seem to be allowing the font to download without blocking.

I.e., my CSS

  @font-face {
        font-family: ...
        format('svg');
        font-weight: normal;
        font-style: normal;
        text-decoration:none;
        font-display:swap;}

But the GTMetrix waterfall shows that this font is still blocking.

Waterfall enter image description here

Blocking detail

enter image description here

Have I misunderstood the display:swap or mis read the waterfall?

like image 458
Trebor Avatar asked Jun 16 '20 01:06

Trebor


People also ask

What is font display swap in CSS?

The font display strategy is defined by the user agent. block. Gives the font face a short block period and an infinite swap period. swap. Gives the font face an extremely small block period and an infinite swap period.

Can you use OTF in CSS?

You can use EOT (Embedded OpenType) files for Internet Explorer and either OTF (OpenType) or TTF (TrueType) for the rest. (Additional options include WOFF (Web Open Font Format) and SVG (Scalable Vector Graphics) but we will stick to more common types here.)

What would be the effect of using font display swap?

swap gives the font face a zero second block period and an infinite swap period. This means the browser draws text immediately with a fallback if the font face isn't loaded, but swaps the font face in as soon as it loads.

Can I use font display optional?

You can! That's what font-display: optional; does. It still gives the ~100ms font block period (giving the font a fighting chance to show up on first page view), but after that, the fallback is shown and will not swap. Chances are, the font did ultimately get downloaded, and next page view it will be cached and used.


2 Answers

The css property font-display:swap won't prevent the the blocking during the page load, it only will show a fallback font if the specified is not available yet.

If you want to optimize the fonts loading I recommend you read this post, it explains how to optimize the google fonts loading using the usual CSS link using preconnect or a full optimization using self hosted fonts.

like image 152
emartini Avatar answered Oct 11 '22 09:10

emartini


According to Google devs:

The font swap period occurs immediately after the font block period. During this period, if the font face is not loaded, any element attempting to use it must instead render with a fallback font face. If the font face successfully loads during the swap period, the font face is then used normally.

swap gives the font face a zero second block period and an infinite swap period. This means the browser draws text immediately with a fallback if the font face isn’t loaded, but swaps the font face in as soon as it loads. Similar to block, this value should only be used when rendering text in a particular font is important for the page, but rendering in any font will still get a correct message across. Logo text is a good candidate for swap since displaying a company’s name using a reasonable fallback will get the message across but you’d eventually use the official typeface.

I see in your font format SVG, I usually use WOFF or WOFF2. Maybe that specific font in SVG is impacting negatively the network performance of your website.

like image 42
AleDP Avatar answered Oct 11 '22 10:10

AleDP