When to use rel=“preload”? Why is preloading fonts/FontAwesome a good idea?

丶灬走出姿态 提交于 2020-12-12 02:43:07

问题


I got this recommendation, in Google Pagespeed:



The 'learn more'-link leads to 404. I tried figuring out, why this is supposed to save me 7.08 seconds, but can't find it.

I would assume that loading 10 icons on a page would be the very last priority?! Isn't images, other fonts and scripts more important?

Or is somehow stalling something, that these fonts aren't loaded?


I can see in my Network-tab, that if I load the fonts as such:

<link rel="preload" href="css/fontawesome.css" as="style" onload="this.rel='stylesheet'">

... that is (true enough) becomes a priority and gets loaded before any other thing. But do I really want that?


UPDATE

I know that this can be interpreted as an SEO-question, since it origins from Google Pagespeed. But it's not. It's a 'How to build a good website'-questions. Ranking on Google doesn't matter. The experience on the site matters.


回答1:


You will often see this recommendation if you use @font-face to load fonts in.

To understand why you get this recommendation you have to consider how the browser receives and parses information.

  1. HTML is downloaded, browser looks at all the assets to download it found in the HTML and starts downloading them and parsing them.
  2. The browser discovers a CSS file and downloads it. When that CSS file has downloaded and been parsed your browser finds a reference to your 'font-awesome' font and then adds that to the list of things to download.
  3. The browser downloads that font, but a lot later than it is needed.

By adding preload to the item your order changes to HTML first, then CSS and the font-awesome font at the same time, meaning a key asset is loaded earlier.

Why is this important?

To understand why this is important you need to understand 'critical requests' - those are all the assets required in order to render the 'above the fold' content.

Above the fold content is content you can see without scrolling the page.

Now if you have any icon showing in this 'above the fold' content then your font-awesome font becomes part of your 'critical request chain' - i.e. content that is essential to paint everything above the fold.

By using preload you get the font delivered sooner (2 steps not 3 as illustrated earlier), so your above the fold content can be rendered sooner and so your site appears to load faster - this is a major factor in PSI scoring and real-world conversion rate improvements.

Should I use rel="preload" then?

In most circumstances yes you should if it is recommended, it reduces your critical request chain depth and normally results in faster loading times. However you do need to check your critical request chain to ensure it isn't a false positive (PSI isn't perfect).

The easiest way to check is to open developer tools, enable 3G throttling on the network tab and then see if the page displays faster with or without preload.

Is it my best option for the scenario given in the question?

In this example, no, but only because font-awesome is not a good idea in general.

What you really want to do is get rid of font-awesome entirely. Icon Fonts are an out-dated and terrible practice us web developers have adopted that just won't go away.

Instead of loading a 50kb+ file (for each 'weight' of font-awesome you use) plus 30kb of CSS why not use inline SVGs instead.

Inline SVGs have several advantages, but the key ones are:-

  1. As they are inlined in the HTML you remove at least one network request (normally 2-3) - great for speed.
  2. They are tiny - a typical icon is less than 1kb unzipped - with 10 icons you said you use that is 10kb total before zipping. Compare that to 180kb zipped for font-awesome fonts, CSS etc. and you can see the performance improvement.
  3. As you have inlined your icons within your HTML you reduce your 'critical request chain' length, so you can get to sub 1 second initial page renders (obviously you need to inline all of your critical CSS required for the above the fold as well.)
  4. The most important reason - people use custom stylesheets on your websites. For example people with dyslexia may prefer a certain font as it is easier to read, so they may force a site to use that font. Your beautiful 'font icons' become the dreaded 'missing character square box of doom' - which makes it really difficult to know what they are clicking. Accessibility is becoming more and more important!

Note on point 2 - the reason icon fonts are so large is they contain hundreds of icons. It is possible to reduce them to be slightly smaller that inline SVGs but that kind of optimisation often gets over-looked and is actually more time consuming than simply inlining and referencing SVGs. I just thought I would add this for completeness.



来源:https://stackoverflow.com/questions/60414566/when-to-use-rel-preload-why-is-preloading-fonts-fontawesome-a-good-idea

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!