Architecting Resilient Frontends

A presentation at jQuery UK in March 2015 in Oxford, UK by Andy Hume

Slide 1

Slide 1

RESILIENT FRONT- ENDS @andyhume ARCHITECTING jQuery UK, 2015

Slide 2

Slide 2

Slide 3

Slide 3

Slide 4

Slide 4

Slide 5

Slide 5

Slide 6

Slide 6

Slide 7

Slide 7

waiting for fonts.com

Slide 8

Slide 8

Unreliable network

http://

Slide 9

Slide 9

Resilient web

http://

Slide 10

Slide 10

Progressive enhancement Progressive enhancement

http://www.flickr.com/photos/8040811@N06/3167877765

Slide 11

Slide 11

Content THREE STAGES Enhancement Leftovers

Slide 12

Slide 12

Content THREE STAGES Enhancement Leftovers

Slide 13

Slide 13

Content THREE STAGES Enhancement Leftovers

Slide 14

Slide 14

Content THREE STAGES Enhancement Leftovers

Slide 15

Slide 15

Content Enhancement Leftovers

Slide 16

Slide 16

Content Enhancement Leftovers

Slide 17

Slide 17

Time to screen Key performance metric Aiming for 1s on mobile Understand the network

Understand the browser

Slide 18

Slide 18

Time to screen: The network DNS lookup TCP connect HTTP request Server time HTTP resp

Slide 19

Slide 19

Time to screen: The network DNS lookup TCP connect HTTP request Server time HTTP resp

Slide 20

Slide 20

Time to screen: The network DNS lookup TCP connect HTTP request Server time HTTP resp

Slide 21

Slide 21

Time to screen: The network DNS lookup TCP connect HTTP request Server time HTTP resp SSL Handshake

Slide 22

Slide 22

Time to screen: The network DNS lookup TCP connect HTTP request Server time HTTP resp HTTP Redirect SSL Handshake

Slide 23

Slide 23

Time to screen: The network DNS lookup TCP connect HTTP request Server time HTTP resp HTTP Redirect SSL Handshake

Slide 24

Slide 24

Time to screen: The network DNS lookup TCP connect HTTP request Server time HTTP resp HTTP Redirect SSL Handshake

Slide 25

Slide 25

Time to screen: The network DNS lookup TCP connect HTTP request Server time HTTP resp HTTP Redirect SSL Handshake

Slide 26

Slide 26

Time to screen: The network DNS lookup TCP connect HTTP request Server time HTTP resp HTTP Redirect SSL Handshake

Slide 27

Slide 27

Eliminate redirects Flush the document early Prefetch DNS But mostly, eli minate redirects Time to screen: The network

Slide 28

Slide 28

Time to screen: The browser Single threaded event loop 1. Construct DOM tree from HTML 2. Construct render tree from DOM tree and

stylesheets 3. Layout ! paint

Slide 29

Slide 29

Time to screen: Start render HTML parser DOM tree Layout Paint Render tree

Slide 30

Slide 30

Time to screen: Start render HTML parser DOM tree Layout Paint Render tree

Slide 31

Slide 31

Time to screen: Start render HTML parser DOM tree Layout Paint Render tree

Slide 32

Slide 32

Time to screen: Start render HTML parser DOM tree Layout Paint Render tree

Slide 33

Slide 33

Blocking: remote script < script

src

"app.js"

</ script

Slide 34

Slide 34

Blocking: inline script waiting on remote CSS < style

rel

"stylesheet" href

"app.css" />

< script

var rules = window . styleSheets [ 0 ].cssRules;

</ script

Slide 35

Slide 35

Blocking: remote script (solution 1)

</ div

< script

src

"app.js"

</ script

</ body

</ html

Slide 36

Slide 36

Blocking: remote script (solution 2) < script

var script = document.createElement('script'); script. src = "app.js"; document.head.appendChild(script); </ script

Slide 37

Slide 37

Blocking: remote script (solution 2) < script

var script = document.createElement('script'); script. src = "app.js"; document.head.appendChild(script); </ script

HIDDEN FROM PRE-PARSER!

Slide 38

Slide 38

Blocking: remote script (solution 3) < script

src

“app.js" async

</ script

Slide 39

Slide 39

Time to screen: Start render HTML parser DOM tree Layout Paint Render tree

Slide 40

Slide 40

Time to screen: Start render HTML parser DOM tree Layout Paint Render tree

Slide 41

Slide 41

Time to screen: Start render HTML parser DOM tree Layout Paint Render tree

Slide 42

Slide 42

Time to screen: Start render HTML parser DOM tree Layout Paint Render tree

Slide 43

Slide 43

Blocking: fetching stylesheets < style

rel

"stylesheet" href

"app.css" />

Slide 44

Slide 44

Blocking: fetching stylesheets (solution) < head

< style

// Core styles in the head of the document.

body {

background : grey ;

color : #3A2FDE ; } </ style

</ head

< body

Slide 45

Slide 45

Blocking: critical CSS generators https://github.com/pocketjoso/penthouse

http://jonassebastianohlsson.com/ criticalpathcssgenerator/

https://github.com/filamentgroup/criticalcss

Slide 46

Slide 46

Recap: What blocks render? Remote scripts that need to be executed synchronously Inline script waiting on stylesheet fetch

Stylesheet fetch for relevant media type/query

Slide 47

Slide 47

Recap: Fault isolation …or swimlaning Avoid synchronous dependencies Uptime is not binary

Slide 48

Slide 48

Blocking web fonts

Slide 49

Slide 49

Blocking web fonts

Slide 50

Slide 50

Blocking web fonts HTML CSS FONT FONT FONT START RENDER

Slide 51

Slide 51

Blocking web fonts HTML CSS FONT FONT FONT START RENDER TEXT RENDER

Slide 52

Slide 52

Blocking web fonts HTML CSS FONT FONT FONT START RENDER TEXT RENDER

Slide 53

Slide 53

Blocking web fonts FALLBACK BLOCKING BLOCKING + TIMEOUT Internet Explorer Safari Mobile Safari Chrome Opera (Blink) Firefox Opera (Presto)

Slide 54

Slide 54

Web font loader Provide control over font loading Remove fonts from the critical path Make cross-browser behaviour consistent

Slide 55

Slide 55

Web font loader < style

rel

"stylesheet" href

"myfonts.css" />

Slide 56

Slide 56

Web font loader < style

rel

"stylesheet" href

"myfonts.css" />

Slide 57

Slide 57

Web font loader < style

rel

"stylesheet" href

"myfonts.css" />

var WebFontConfig

{ custom: { families: [ 'Clarendon' , 'Clarendon Bold' ],
urls: [ '/myfonts.css' ]

} }; < script

src

“//ajax.googleapis.com/webfonts.js" async

</ script

Slide 58

Slide 58

Web font loader < body class

"clarendon-loading"

< body class

"clarendon-loaded"

Slide 59

Slide 59

Web font loader < body class

"clarendon-loading"

< body class

"clarendon-loaded"

h1 {

font-family : georgia, serif ; } .clarendon-loaded h1 {

font-family : Clarendon, georgia, serif ;

}

Slide 60

Slide 60

CSS Font Loading Module document .fonts.load( ‘Clarendon’ ).then( function () {

document .documentElement.className += ‘clarendon-loaded';

} h1 {

font-family : georgia, serif ; } .clarendon-loaded h1 {

font-family : Clarendon, georgia, serif ;

}

Slide 61

Slide 61

http://

Slide 62

Slide 62

Median of nine test runs http://www.webpagetest.org/result/130908_K2_796/

Slide 63

Slide 63

Median of nine test runs iPhone 4, iOS 5.1 http://www.webpagetest.org/result/130908_K2_796/

Slide 64

Slide 64

Median of nine test runs 3G (1.6Mps, 300ms RTT) iPhone 4, iOS 5.1 http://www.webpagetest.org/result/130908_K2_796/

Slide 65

Slide 65

Slide 66

Slide 66

Thank-you! lanyrd.com/sddxxk

@andyhume Creative Commons Licensed Attribution, Non-Commercial, Share Alike cc