Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Exploring Critical Rendering Path

Exploring Critical Rendering Path

This guide will not bring you a magic formula to optimize critical render path. When the subject is web performance: there's no magic formula. Analyze performance is careful and meticulous process, and it can bring different results based on various existing variables.

Raphael Amorim

October 08, 2016
Tweet

More Decks by Raphael Amorim

Other Decks in Programming

Transcript

  1. Raphael Amorim This guy seems like a nice person, but

    he doesn’t. Seriously. He likes topics related to JavaScript, Python, Clojure, WebGL, Algorithms and sometimes force some git push.
 Working most part of his time in useless open source projects. Works in globo.com, loves to create useful tiny modules to daily use. @raphamundi
  2. HTML markup is transformed into a Document Object Model (DOM)

    CSS markup is transformed into a CSS Object Model (CSSOM)
  3. HTML markup is transformed into a Document Object Model (DOM)

    CSS markup is transformed into a CSS Object Model (CSSOM) DOM and CSSOM are independent data structures
  4. HTML markup is transformed into a Document Object Model (DOM)

    CSS markup is transformed into a CSS Object Model (CSSOM) DOM and CSSOM are independent data structures Bytes ! characters ! tokens ! nodes ! object model
  5. <html> <head> <meta charset=“utf-8"> <meta name="viewport" content="width=device-width,initial-scale=1"> <title>The Astronaut</title> <!--

    Stylesheet --> <link href="style.css" rel="stylesheet"> </head> <body> <p>Neil Armstrong</p> <p><img src=“just-a-neil-picture.jpg”></p> </body> </html> How does the browser process this page?
  6. While browser was mounting the DOM. It encountered a link

    tag in the head section referencing an external CSS
  7. We repeat the HTML process, but for CSS instead of

    HTML: Bytes ! characters ! tokens ! nodes ! CSSOM
  8. The DOM and CSSOM trees are combined to form the

    render tree Render tree contains only the nodes required to render the page
  9. The DOM and CSSOM trees are combined to form the

    render tree Render tree contains only the nodes required to render the page Layout computes the exact position and size of each object
  10. Captures all the visible DOM content on the page and

    all the CSSOM style information for each node
  11. p img body CSSOM color: black background: #FFF color: black

    *font-size: 14px *user agent styles *font-weight: normal padding: 10px
  12. img body Render Tree color: black background: #FFF color: black

    *font-size: 14px *user agent styles *font-weight: normal padding: 10px p “Neil Arm…”
  13. Captures the exact position and size of each element within

    the viewport <html> <head> <meta name="viewport" content="width=device-width,initial- scale=1"> <title>
 My application
 </title> </head> <body> <div style="width: 100%"> <div style="width: 50%">
 JavaScript Rocks!
 </div> </div> </body> </html>
  14. Captures the exact position and size of each element within

    the viewport <html> <head> <meta name="viewport" content="width=device-width,initial- scale=1"> <title>
 My application
 </title> </head> <body> <div style="width: 100%"> <div style="width: 50%">
 JavaScript Rocks!
 </div> </div> </body> </html> viewport size=device-width JavaScript Rocks!
  15. After know what nodes are visible, and get their computed

    styles and geometry, becomes Paint stage.
  16. Time required to perform render tree construction, layout and paint

    varies based on the size of the document, the applied styles, and the device it is running on.
  17. The goal of optimizing the critical rendering path is to

    allow the browser to paint the page as quickly as possible.
  18. Let’s dive in a simple request. Considering a use of

    regular 3G, the network roundtrip (propagation latency) to the server will cost 100ms - 750kb/s ~ 250kb/s.
  19. <html> <head> <meta name="viewport" 
 content="width=device-width,initial-scale=1"> <title>Some Random Page</title> <link

    href="style.css" rel="stylesheet"> <script src="jquery.js"></script> </head> <body> <div><img src="polemic-photo.jpg"></div> <script src="main.js"></script> </body> </html>
  20. Once the HTML content becomes available, the browser has to

    parse the bytes, convert them into tokens, and build the DOM tree
  21. 1# Note: 
 JavaScript wait til CSS files are downloaded

    and parsed Even with inline scripts?
  22. 1# Note: 
 JavaScript wait til CSS files are downloaded

    and parsed Even with inline scripts? Inline scripts force browsers to intends to know what that script does. It blocks the CSS parse if it's placed above link or style tags
  23. 2# Note: 
 Images doesn’t block the initial render of

    the page (including domContentLoaded event).
  24. When we talk about the critical rendering path we are

    typically talking about the HTML markup, CSS and JavaScript
  25. Make critical assets as small as possible by minifying and

    compressing both the html and css (obfuscation process)
  26. Specifies that the script will be executed asynchronously as soon

    as it is available 
 
 (only for external scripts)
  27. <html> <head> <meta name="viewport" 
 content="width=device-width,initial-scale=1"> <title>Some Random Page</title> <link

    href="style.css" rel="stylesheet"> <script src="jquery.js"></script> </head> <body> <div><img src="polemic-photo.jpg"></div> <script src=“main.js"></script> </body> </html>
  28. <html> <head> <meta name="viewport" 
 content="width=device-width,initial-scale=1"> <title>Some Random Page</title> <link

    href="style.css" rel="stylesheet"> <script src="jquery.js"></script> </head> <body> <div><img src="polemic-photo.jpg"></div> <script src=“main.js"></script> </body> </html>
  29. <html> <head> <meta name="viewport" 
 content="width=device-width,initial-scale=1"> <title>Some Random Page</title> <link

    href="style.css" rel="stylesheet"> <script src="jquery.js"></script> </head> <body> <div><img src="polemic-photo.jpg"></div> <script src=“main.js"></script> </body> </html> DOMContentLoaded: 1.73s
  30. <html> <head> <meta name="viewport" 
 content="width=device-width,initial-scale=1"> <title>Some Random Page</title> <link

    href="style.css" rel="stylesheet"> <script src="jquery.js"></script> </head> <body> <div><img src="polemic-photo.jpg"></div> <script src=“main.js” async></script> </body> </html>
  31. <html> <head> <meta name="viewport" 
 content="width=device-width,initial-scale=1"> <title>Some Random Page</title> <link

    href="style.css" rel="stylesheet"> <script src="jquery.js"></script> </head> <body> <div><img src="polemic-photo.jpg"></div> <script src=“main.js” async></script> </body> </html>
  32. <html> <head> <meta name="viewport" 
 content="width=device-width,initial-scale=1"> <title>Some Random Page</title> <link

    href="style.css" rel="stylesheet"> <script src="jquery.js"></script> </head> <body> <div><img src="polemic-photo.jpg"></div> <script src=“main.js"></script> </body> </html> DOMContentLoaded: 191ms
  33. Pros: Specify which scripts will be loaded according to the

    interaction Better script dependencies management
  34. A good approach is to inline only the critical css,

    and downloading the remaining css async
  35. npm i fontfaceonload FontFaceOnload("My Custom Font Family", { success: function()

    { document.documentElement.className.add(“my-custom-font-family”); } });