Loading CSS without blocking render

Source: Internet
Author: User

The principles behind these techniques aren ' t new. Filament group, for example, has published great content on loading CSS and fonts. I ' ve written this article to document my thoughts and ideas for loading non-blocking resources.

The trick to triggering an asynchronous stylesheet download are to use a <link> element and set an invalid value for the attribute (I ' m using media="none" , but any value would do). When a media query evaluates to FALSE, the browser would still download the stylesheet, but it won ' t wait for the content T o be available before rendering the page.

<link rel="stylesheet" href="css.css" media="none">

Once The stylesheet has finished downloading the media attribute must being set to a valid value so the style rules would be a Pplied to the document. The event is used to switch the property to onload media all :

<link rel="stylesheet" href="css.css" media="none" onload="if(media!=‘all‘)media=‘all‘">

This method of loading CSS would deliver useable content to visitors much quicker than, the standard approach. Critical CSS can still is served with the usual blocking approach (or can inline it for ultimate performance) and non- Critical styles can be progressively downloaded and applied later in the parsing/rendering process.

This technique uses JavaScript, but can cater for non-javascript browsers by wrapping the equivalent blocking  <link>  elements in a  <noscript>  element:

<link rel= "stylesheet" href= "css.css" media= "none" onload=" if (media!= ' all ') media= ' all ' ">< Noscript><link rel= " Stylesheet "href=" css.css "> </noscript>              

There is a side-effect to this technique. Once a non-blocking stylesheet has finished downloading the document would be repainted to reflect any new rules it defines . Injecting new styles into the page can trigger content reflows, but this is only really a issue for the first Pag e load with an unprimed cache. As with all things related to performance, you'll need to make a judgement call on when the need to control a reflow Outwe Ighs the potential speed gain.

Using non-blocking CSS to load fonts

Fonts is an issue for first-paint performance, they is a blocking resource and can render text invisible while they down Load. Using the Non-blocking link example above, it ' s possible to download a stylesheet containing font data in the background, Unblocking the page render:

<link rel="stylesheet" href="main.css"><link rel="stylesheet" href="font.css" media="none" onload="if(media!=‘all‘)media=‘all‘">

font.cssContains a base64 encoded WOFF version of the Merriweather font.

 @font-face { Font-family: Merriweather; Font-style: Normal; Font-weight: 400; Src: Local ( Span class= "code__string" > ' Merriweather ' )  URL  ( ' data:application/x-font-woff;charset=utf-8;base64,... ' Span class= "Code__punc" >) }          

main.cssContains all of the rules required to style the site. Here's the font declaration:

{  font-family: Merriweather, "Lucida Grande", ...;}

While the font was downloading, the first matching fallback font (lucida Grande, in this case) was used to render t He page content. Once the font stylesheet is applied,Merriweather would be used. I try to ensure the fallback shares similar layout characteristics to the preferred font, so that the inevitable reflow is As subtle as possible.

I ' m testing blocking vs non-blocking using my Google Analytics Debugger site in Chrome over a simulated 3G connection. Local testing produces the following network graphs; Notice the DOMContentLoaded event fires around 450ms earlier and assets begin downloading sooner when non-blocking is used:

Simulated 3G network graph. Top shows blocking fonts. Bottom shows non-blocking fonts.

Deploying this to a test server and running Webpagetest with 3G connection shaping produces the following timeline:

3G timeline. Top shows blocking fonts. Bottom shows non-blocking fonts.

Both methods take 2.8 seconds to completely render the page and the Non-blocking method causes painting to being a se Cond earlier than the normal blocking approach. Running the same test with the main stylesheet inlined shows a 0.7 second gain when non-blocking CSS was used to serve the Font

3G timeline with main CSS inlined. Top shows blocking fonts. Bottom shows non-blocking fonts.

This technique does work well for fonts but I recommend keeping a eye on the new CSS Font Loading Module, which gives far Greater control over font loading.

Summary

Loading fonts is one example of applying this non-blocking technique, but it could also was used for other purposes, such a s separating JavaScript enhanced styles from core CSS.

I ' ve started to experiment with the idea of breaking up CSS into scaffolding (core layout) and presentation (Everything El SE), allowing vital page layout to block the page render and has the visual styles arrive later.

Thanks to Mathias Bynens for taking the time to share a shortened version of the <link> onload handler.

Loading CSS without blocking render

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.