Download - Automating Faster Page Rendering - step by step from start to finish

Transcript
Page 1: Automating Faster Page Rendering - step by step from start to finish

/ Jonas Ohlsson @pocketjoso

AutomatingFaster PageRendering

— Step by step from start to finish

Page 2: Automating Faster Page Rendering - step by step from start to finish

Today's talk

Page 3: Automating Faster Page Rendering - step by step from start to finish
Page 4: Automating Faster Page Rendering - step by step from start to finish
Page 5: Automating Faster Page Rendering - step by step from start to finish
Page 6: Automating Faster Page Rendering - step by step from start to finish

Start Render TimeWhat is the browser waiting for?

Page 7: Automating Faster Page Rendering - step by step from start to finish

JS & CSSBlocking assets

Page 8: Automating Faster Page Rendering - step by step from start to finish

HTTP Requests are evil(In current protocol)

Page 9: Automating Faster Page Rendering - step by step from start to finish

Reduce Render StartTime: How to

<html> <head>... <link rel="stylesheet" href="css/styles.css"> <script src="js/scripts.js"></script></head><body><!--Blocking HTTP requests AFTER DOM content--></body></html>

Page 10: Automating Faster Page Rendering - step by step from start to finish

Reduce Render StartTime: How to

<html> <head>...<!--Inline CSS to avoid HTTP request. Only critical css for page though!--> <style> body { color: #202020; } </style></head><body><!--Page content here--> <link rel="stylesheet" href="css/styles.css"> <script src="js/scripts.js"></script></body></html>

Page 11: Automating Faster Page Rendering - step by step from start to finish

What is critical (css)?

Page 12: Automating Faster Page Rendering - step by step from start to finish

“Styles needed to render immediately visibleDOM content.”

Me, right now

Page 13: Automating Faster Page Rendering - step by step from start to finish
Page 14: Automating Faster Page Rendering - step by step from start to finish
Page 15: Automating Faster Page Rendering - step by step from start to finish

Step 1. GenerateCreating the critical CSS

Page 16: Automating Faster Page Rendering - step by step from start to finish

Manually?No way!

Page 17: Automating Faster Page Rendering - step by step from start to finish

Scripting//for each selector in full CSS:var elements = document.querySelectorAll(".myClass");//for each element on page matchedfor (var i=0; i < elements.length; i++){ //if "above the fold" if (elements[i].getBoundingClientRect().top > window.innerHeight) { //keep CSS rules that use this selector keepThis = true; }}

Page 18: Automating Faster Page Rendering - step by step from start to finish

Slightly more complicated..

@-Rules (@font-face, @media, @keyframe...)

pseudo-selectors (:before, :hover ..)

others.. (input:-moz-focus-inner, ::-webkit-input-

placeholder)

Page 19: Automating Faster Page Rendering - step by step from start to finish

Perfection

Necessary for automation

Page 20: Automating Faster Page Rendering - step by step from start to finish

Demohttp://jonassebastianohlsson.com/criticalpathcssgenerator

Page 21: Automating Faster Page Rendering - step by step from start to finish

Step 2. BuildAutomating the critical css generation

Page 22: Automating Faster Page Rendering - step by step from start to finish

PenthouseCLI, Node, Grunt, Gulp (Thanks !)@kopseng

https://github.com/pocketjoso/penthouse

Page 23: Automating Faster Page Rendering - step by step from start to finish

Grunt generate setuppenthouse: { task1 : { css : 'css/jso.min.css', url : 'http://localhost/jso/index.php', outfile : 'index.php.css', width : 1300, height : 900 }}

Page 24: Automating Faster Page Rendering - step by step from start to finish

Grunt generate setuppenthouse: { task1 : { css : 'css/jso.min.css', url : 'http://localhost/jso/index.php', outfile : 'index.php.css', width : 1300, height : 900 }, task2 : { css : 'css/jso.min.css', url : 'http://localhost/jso/work/index.php', outfile : 'work/index.php.css', width : 1300, height : 900 } /*..etc !*/}

Page 25: Automating Faster Page Rendering - step by step from start to finish

Step 3. ServeMaking use of the critical CSS

Page 26: Automating Faster Page Rendering - step by step from start to finish

PHP serve setup<head><?php//get file on server for this page$pagefile = $_SERVER['DOCUMENT_ROOT'] . $_SERVER['PHP_SELF'];//answer like 'C:/xampp/htdocs/jso/index.php'

//check if there is a corresponding critical css file$critcssfile = $pagefile . ".css";if (file_exists($critcssfile)) { $USE_CRITICAL_CSS = true;?> <!--inline critical path CSS--> <style><?php include($critcssfile);?></style><?php } else { ?> <!--fallback - no critical css found for page --> <link rel="stylesheet" href="/jso/css/jso.min.css" media="all"><?php } ?></head>

Page 27: Automating Faster Page Rendering - step by step from start to finish

PHP footer<!--towards end of page..-><!--CSS - if using Critical Path CSS on page--><?php if($USE_CRITICAL_CSS){?><link rel="stylesheet" href="/jso/css/jso.min.css" media="all"><?php } ?></body></html>

Page 28: Automating Faster Page Rendering - step by step from start to finish

THE END

Page 29: Automating Faster Page Rendering - step by step from start to finish

LinksCritical Path Css generator tools

Penthouse [https://github.com/pocketjoso/penthouse]

Online Critical path css generator

[http://jonassebastsianohlsson.com/criticalpathcssgenerator]

Critical (extract & inline critical css automation)

[https://github.com/addyosmani/critical]

Page 30: Automating Faster Page Rendering - step by step from start to finish

LinksPerformance testing

WebPageTest [http://webpagetest.org]

PageSpeed Insights [http://developers.google.com/speed/pagespeed/insights/]

Pingdom Website Speed Test [http://tools.pingdom.com/fpt/]

Page 31: Automating Faster Page Rendering - step by step from start to finish

LinksGoing further

loadCSS (faster than using links, for full footer CSS)

[https://github.com/filamentgroup/loadCSS]

basket.js (store CSS/JS in localStorage rather than browser

cache) [http://addyosmani.github.io/basket.js/]

How to make RWD sites load fast as heck

[http://filamentgroup.com/lab/performance-rwd.html]