Presentation for WordCamp Europe 2016

A presentation at WordCamp Europe 2016 in June 2016 in Vienna, Austria by Peter Wilson

Slide 1

Slide 1

Wed 29 Oct 1969 ~ ucla$ ░

Slide 2

Slide 2

Wed 29 Oct 1969 ~ ucla$ ░

Slide 3

Slide 3

Wed 29 Oct 1969 ~ ucla$ l o

░
								
gin

Slide 4

Slide 4

Wed 29 Oct 1969 ~ ucla$ l o

░ ░

Slide 5

Slide 5

Seven times faster: 
 a study in front-end optimisation Peter Wilson • peterwilson.cc • @pwcc

Slide 6

Slide 6

Slide 7

Slide 7

WordPress is slow

Slide 8

Slide 8

Front-end code is slow

Slide 9

Slide 9

Bytes on the page

Slide 10

Slide 10

Total page size (MB) Nov 2011 - March 2016 0.5 1.0 1.5 2.0 2.5 1 Nov '11 15 May '12 1 Dec '12 15 Jun '13 1 Jan '14 15 Jul '14 1 Feb '15 15 Aug '15 1 Mar '16 httparchive.org/trends.php , March 2016

Slide 11

Slide 11

Assets per page Nov 2011 - March 2016 20 40 60 80 100 120 1 Nov '11 15 May '12 1 Dec '12 15 Jun '13 1 Jan '14 15 Jul '14 1 Feb '15 15 Aug '15 1 Mar '16 httparchive.org/trends.php , March 2016

Slide 12

Slide 12

Webfonts Multiple of Nov 2011 (bytes) 2 4 6 8 10 12 14 16 18 1 Nov '11 15 May '12 1 Dec '12 15 Jun '13 1 Jan '14 15 Jul '14 1 Feb '15 15 Aug '15 1 Mar '16 httparchive.org/trends.php , March 2016

Slide 13

Slide 13

Load time httparchive.org/trends.php , March 2016

Slide 14

Slide 14

Load time Start render: 4.2 seconds

Slide 15

Slide 15

Load time Visually complete: 12.7 seconds

Slide 16

Slide 16

Load time Document complete: 15.23 sec

Slide 17

Slide 17

Two seconds

Slide 18

Slide 18

Walmart conversion rate slideshare.net/devonauerswald/walmart-pagespeedslide 0 - 1 1 - 2 2 - 3 3 - 4 4 - 5 5 - 6 6 - 7 7 - 8 8 - 9 9 - 10 10 - 11 11 - 12 12 - 13 13 - 14 14 - 15 15+

Slide 19

Slide 19

The average site kills conversions.

Slide 20

Slide 20

Measure a starting point

Slide 21

Slide 21

Slide 22

Slide 22

WebPageTest - webpagetest.org

Slide 23

Slide 23

Key metrics Document Complete Fully Loaded Load Time First Byte Start Render Speed Index DOM Elements Time Requests Bytes In Time Requests Bytes In 3.770s 0.771s 1.789s 1834 397 3.770s 27 635 KB 4.072s 33 661 KB WebPageTest - webpagetest.org

Slide 24

Slide 24

Timeline view

Slide 25

Slide 25

Comparison timelines Additional blocking request in HTML Header Timed using WebPageTest - pwcc.cc/wceu/blocking 0.5s 1.0s 1.5s 2.0s 0% 0% 0% 100% 0% 86% 100% Blocked Unblocked

Slide 26

Slide 26

53 / 100 Our PageSpeed score is you won’t believe how 
 much it’s costing

Slide 27

Slide 27

What will get my visitors reading quickest? “ ”

Slide 28

Slide 28

Doing it wrong

Slide 29

Slide 29

JavaScript in the footer

Slide 30

Slide 30

All visitors have JavaScript disabled while it downloads. – Every progressive enhancement advocate ever “ ”

Slide 31

Slide 31

wp_enqueue_script(

'pwcc-scripts' , // handle

'/functions.js' , // source

null , // no dependancies

'20160624-26' , // version

true

// load in footer ); JavaScript, the WordPress way

Slide 32

Slide 32

wp_enqueue_script(

'pwcc-scripts' , // handle

'/functions.js' , // source

null , // no dependancies

'20160624-26' , // version

true

// load in footer );

true

// load in footer JavaScript, the WordPress way

Slide 33

Slide 33

wp_enqueue_script(

'pwcc-scripts' , // handle

'/functions.js' , // source

array ( 'jquery' ), // jQuery loads automatically

'20160624-26' , // version

true

// load in footer ); jQuery, the WordPress way

Slide 34

Slide 34

wp_enqueue_script(

'pwcc-scripts' , // handle

'/functions.js' , // source

array ( 'jquery' ), // jQuery loads automatically

'20160624-26' , // version

true

// load in footer );

array ( 'jquery' ), // jQuery loads automatically jQuery, the WordPress way

Slide 35

Slide 35

< html

< head

	<

script

src

'jquery.js'

</ script

<!--32kB-->
	<

script

src

'jquery-migrate.js'

</ script

<!--4.4kB-->

</ head

< body

<!-- Site content. -->
	<

script

src

'/functions.js'

</ script

</ body

jQuery, doing_it_wrong()

Slide 36

Slide 36

< html

< head

	<

script

src

'jquery.js'

</ script

<!--32kB-->
	<

script

src

'jquery-migrate.js'

</ script

<!--4.4kB-->

</ head

< body

<!-- Site content. -->
	<

script

src

'/functions.js'

</ script

</ body

</ html

jQuery, doing_it_wrong()

Slide 37

Slide 37

The WordPress way blocks rendering Daniel Zedda (CC) - flic.kr/p/a6wwAh

Slide 38

Slide 38

function

pwcc_jquery_to_footer () {

if ( is_admin() )

return ;

w p _ s c r i p t _ a d d _ d a t a ( 	

'jquery' , 'group' , 1 ); w p _ s c r i p t _ a d d _ d a t a ( 'jquery-core' , 'group' , 1 ); w p _ s c r i p t _ a d d _ d a t a ( 'jquery-migrate' , 'group' , 1 ); } add_action( 'wp' , 'pwcc_jquery_to_footer' ); jQuery, doing_it_wrong()

Slide 39

Slide 39

function

pwcc_jquery_to_footer () {

if ( is_admin() )

return ;

w p _ s c r i p t _ a d d _ d a t a ( 	

'jquery' , 'group' , 1 ); w p _ s c r i p t _ a d d _ d a t a ( 'jquery-core' , 'group' , 1 ); w p _ s c r i p t _ a d d _ d a t a ( 'jquery-migrate' , 'group' , 1 ); } add_action( 'wp' , 'pwcc_jquery_to_footer' ); w p _ s c r i p t _ a d d _ d a t a ( 'jquery' , 'group' , 1 ); w p _ s c r i p t _ a d d _ d a t a ( 'jquery-core' , 'group' , 1 ); w p _ s c r i p t _ a d d _ d a t a ( 'jquery-migrate' , 'group' , 1 ); jQuery, doing_it_wrong()

Slide 40

Slide 40

function

pwcc_jquery_to_footer () {

if ( is_admin() )

return ;

w p _ s c r i p t _ a d d _ d a t a ( 	

'jquery' , 'group' , 1 ); w p _ s c r i p t _ a d d _ d a t a ( 'jquery-core' , 'group' , 1 ); w p _ s c r i p t _ a d d _ d a t a ( 'jquery-migrate' , 'group' , 1 ); } add_action( 'wp' , 'pwcc_jquery_to_footer' ); w p _ s c r i p t _ a d d _ d a t a ( 'jquery' , 'group' , 1 ); w p _ s c r i p t _ a d d _ d a t a ( 'jquery-core' , 'group' , 1 ); w p _ s c r i p t _ a d d _ d a t a ( 'jquery-migrate' , 'group' , 1 ); jQuery, doing_it_wrong()

Slide 41

Slide 41

< html

< head

	<

script

src

'jquery.js'

</ script

<!--32kB-->
	<

script

src

'jquery-migrate.js'

</ script

<!--4.4kB-->

</ head

< body

<!-- Site content. -->
	<

script

src

'/functions.js'

</ script

</ body

jQuery, the WordPress way

Slide 42

Slide 42

< html

< head

<!-- HTML Header. -->
</

head

< body

<!-- Site content. -->
	<

script

src

'jquery.js'

</ script

<!--32kB-->
	<

script

src

'jquery-migrate.js'

</ script

<!--4.4kB-->
	<

script

src

'/functions.js'

</ script

</ body

</ html

jQuery, doing_it_wrong()

Slide 43

Slide 43

< html

< head

<!-- HTML Header. -->
</

head

< body

<!-- Site content. -->
	<

script

src

'jquery.js'

</ script

<!--32kB-->
	<

script

src

'jquery-migrate.js'

</ script

<!--4.4kB-->
	<

script

src

'/functions.js'

</ script

</ body

</ html

jQuery, doing_it_wrong() pwcc.cc/wceu/jqueryfooter

Slide 44

Slide 44

A s y n c h r o n o u s J a v a S c r i p t

Slide 45

Slide 45

Asynchronous JavaScript < script

type

'text/javascript'

src

'/path/file.js'

async

</ script

Slide 46

Slide 46

Asynchronous JavaScript < script

type

'text/javascript'

src

'/path/file.js'

async

</ script

async

Slide 47

Slide 47

function

pwcc_async_js ( $tag, $handle ) {

switch ( $handle ) {

case

'pwcc-scripts' :

		$tag	

=

str_replace ( '></script' , ' async></script' , $tag );

}

return $tag; }

Asynchronous JavaScript

Slide 48

Slide 48

function

pwcc_async_js ( $tag, $handle ) {

switch ( $handle ) {

case

'pwcc-scripts' : // Falls through

case

'picturefill' :

		$tag	

=

str_replace ( '></script' , ' async></script' , $tag );

}	

return $tag; }

Asynchronous JavaScript

Slide 49

Slide 49

function

pwcc_async_js ( $tag, $handle ) {

switch ( $handle ) {

case

'pwcc-scripts' : // Falls through

case

'picturefill' :

		$tag	

=

str_replace ( '></script' , ' async></script' , $tag );

}	

return $tag; }

Asynchronous JavaScript

Slide 50

Slide 50

function

pwcc_async_js ( $tag, $handle ) {

switch ( $handle ) {

case

'pwcc-scripts' : // Falls through

case

'picturefill' :

		$tag	

=

str_replace ( '></script' , ' async></script' , $tag );

}	

return $tag; }

add_filter( 'script_loader_tag' , 'pwcc_async_js' , 10 , 2 ); Asynchronous JavaScript

Slide 51

Slide 51

function

pw_async_up () {

	wp_script_add_data(	

'picturefill' , 'group' , 0 ); wp_script_add_data( 'pwcc-scripts' , 'group' , 0 );

} add_action( 'wp_enqueue_scripts' , 'pw_async_up' , 99 ); Asynchronous JS in the header

Slide 52

Slide 52

function

pw_async_up () {

	wp_script_add_data(	

'picturefill' , 'group' , 0 ); wp_script_add_data( 'pwcc-scripts' , 'group' , 0 );

} add_action( 'wp_enqueue_scripts' , 'pw_async_up' , 99 ); Asynchronous JS in the header pwcc.cc/wceu/asyncjs

Slide 53

Slide 53

Browsers initiate requests The blocking nature of HTTP 1

Slide 54

Slide 54

Blockers HTML

Slide 55

Slide 55

Blockers HTML CSS

Slide 56

Slide 56

Blockers HTML CSS

Slide 57

Slide 57

Blockers HTML CSS IMG IMG IMG FONTS

Slide 58

Slide 58

Blockers HTML CSS IMG IMG IMG JS FONTS

Slide 59

Slide 59

Blockers HTML CSS IMG IMG IMG JS IFRAME FONTS

Slide 60

Slide 60

Waterfall WebPageTest - webpagetest.org

Slide 61

Slide 61

WebPageTest - webpagetest.org

Waterfall

Slide 62

Slide 62

Waterfall HTML CSS IMG IMG IMG JS IFRAME FONTS

Slide 63

Slide 63

Waterfall HTML CSS IMG IMG IMG JS IFRAME FONTS

Slide 64

Slide 64

HTTP/2 An aside

Slide 65

Slide 65

Everything you know about performance is now wrong and former best practices are now an anti-pattern and considered harmful.

Slide 66

Slide 66

These titles are considered harmful

Slide 67

Slide 67

Sites using HTTP/2 w3techs.com , March 2016

Slide 68

Slide 68

caniuse.com/http2 , March 2016

Slide 69

Slide 69

caniuse.com/http2 , March 2016

Slide 70

Slide 70

Critical Path CSS

Slide 71

Slide 71

w3.org/TR/preload/

Slide 72

Slide 72

HTTP/2, without critical path CSS HTML CSS

Slide 73

Slide 73

HTTP/1, with critical path CSS HTML CSS

Slide 74

Slide 74

< html

< head

< style

html { font-family : sans-serif ; -ms-text-size-adjust : 100 % ; -webkit-text-size-adjust : 100 % } body { margin : 0 } article , aside , details , figcaption , figure , footer , header , hgroup ,main, nav , section , summary { display : block } audio , canvas , progress , video { display : inline-block ; vertical-align : baseline } audio :not ([controls]){ display : none ; height : 0 }[hidden], template{ display : none } a { background : transparent } a :active , a :hover { outline : 0 } abbr [ title ]{ border-bottom : 1 px

dotted } b , strong { font-weight : bold } dfn { font-style : italic } h1 { font- size : 2 em ; margin : 0.67 em

0 } mark { background : #ff0 ; color : #000 }

Slide 75

Slide 75

HTTP/1, with critical path CSS HTML CSS

Slide 76

Slide 76

HTTP/1, without critical path CSS HTML CSS

Slide 77

Slide 77

CSS, the WordPress way wp_enqueue_style(

'pwcc-style' , // handle

	get_stylesheet_uri(),			

// source

array (), // no dependancies

'20160624-26' , // version

'all'

// media

);

Slide 78

Slide 78

Preloading CSS

Slide 79

Slide 79

Preloading CSS

Slide 80

Slide 80

Preloading CSS Link: </style.css>; rel=preload; as=style ; nopush

Slide 81

Slide 81

Preloading CSS Link: </style.css>; rel=preload; as=style ; nopush

Slide 82

Slide 82

Preloading CSS pwcc_preload_style ( 'pwcc-style' ); // push pwcc_preload_style ( 'pwcc-style', false ); // no push

Slide 83

Slide 83

Server push pwcc_preload_style ( 'pwcc-style' );

Slide 84

Slide 84

Server push if ( is_cached( 'pwcc-style' ) ) { pwcc_preload_style( 'pwcc-style' ); } else

/* file not cached */ {

	pwcc_preload_style(	

'pwcc-style' , false ); }

Slide 85

Slide 85

$version

'20160624-26' ; setcookie ( 'pwcc-style' , $version, 0 , '/' ); Server push

Slide 86

Slide 86

is_cached() function

is_cached ( $handle, $version ) {

if ( $version

$_COOKIE[	$handle	]	)	{	
			

return

true ;

	}	
	

else {

return

false ;

	}	

}

Slide 87

Slide 87

< html

< head

< style

html { font-family : sans-serif ; -ms-text-size-adjust : 100 % ; -webkit-text-size-adjust : 100 % } body { margin : 0 } article , aside , details , figcaption , figure , footer , header , hgroup ,main, nav , section , summary { display : block } audio , canvas , progress , video { display : inline-block ; vertical-align : baseline } audio :not ([controls]){ display : none ; height : 0 }[hidden], template{ display : none } a { background : transparent } a :active , a :hover { outline : 0 } abbr [ title ]{ border-bottom : 1 px

dotted } b , strong { font-weight : bold } dfn { font-style : italic } h1 { font- size : 2 em ; margin : 0.67 em

0 } mark { background : #ff0 ; color : #000 }

Slide 88

Slide 88

Slide 89

Slide 89

pwcc.cc/wceu/loadcss

Slide 90

Slide 90

Register loadCSS wp_register_script(

'pwcc-load-css' , // handle

'/loadcss.js' , // file

array (), // dependencies

"1.2.0" , // version

true

// load in footer

);

Slide 91

Slide 91

< style

type

'text/css'

/* Critical CSS */ </ style

< link

rel

'preload'

href

'/style.css'

as

'style'

onload

'this.rel="stylesheet"'

< noscript

	<

link

rel

'stylesheet'

href

'/style.css' />

</ noscript

HTML: uncached HTTP/1

Slide 92

Slide 92

if ( ! is_cached( 'pwcc-style' ) &&

! is_http2() ) { print_style_inline( 'pwcc-style' ); wp_enqueue_script( 'pwcc-load-css' ); } Inline CSS

Slide 93

Slide 93

if ( ! is_cached( 'pwcc-style' ) &&

! is_http2() ) { print_style_inline( 'pwcc-style' ); wp_enqueue_script( 'pwcc-load-css' ); } Inline CSS pwcc.cc/wceu/rapidcss

Slide 94

Slide 94

< html

< head

< style

html { font-family : sans-serif ; -ms-text-size-adjust : 100 % ; -webkit-text-size-adjust : 100 % } body { margin : 0 } article , aside , details , figcaption , figure , footer , header , hgroup ,main, nav , section , summary { display : block } audio , canvas , progress , video { display : inline-block ; vertical-align : baseline } audio :not ([controls]){ display : none ; height : 0 }[hidden], template{ display : none } a { background : transparent } a :active , a :hover { outline : 0 } abbr [ title ]{ border-bottom : 1 px

dotted } b , strong { font-weight : bold } dfn { font-style : italic } h1 { font- size : 2 em ; margin : 0.67 em

0 } mark { background : #ff0 ; color : #000 }

Slide 95

Slide 95

< link

rel

"stylesheet"

href

"/style.css" /> Cached Stylesheet

Slide 96

Slide 96

< link

rel

'shortcut icon'

href

'/favicon.ico'

< link

rel

'apple-touch-icon'

sizes

'57x57'

href =' /….png'

< link

rel

'apple-touch-icon'

sizes

'114x114'

href =' /….png'

< link

rel

'apple-touch-icon'

sizes

'72x72'

href =' /….png'

< link

rel

'apple-touch-icon'

sizes

'144x144'

href =' /….png'

< link

rel

'apple-touch-icon'

sizes

'60x60'

href =' /….png'

< link

rel

'apple-touch-icon'

sizes

'120x120'

href =' /….png'

< link

rel

'apple-touch-icon'

sizes

'76x76'

href =' /….png'

< link

rel

'apple-touch-icon'

sizes

'152x152'

href =' /….png'

< link

rel

'icon'

type

'image/png'

href =' /….png'

< link

rel

'icon'

type

'image/png'

href =' /….png'

sizes

'160x160'

< link

rel

'icon'

type

'image/png'

href =' /….png'

sizes

'96x96'

< link

rel

'icon'

type

'image/png'

href =' /….png'

sizes

'16x16'

< link

rel

'icon'

type

'image/png'

href =' /….png'

sizes

'32x32'

< meta

name

'msapplication-TileColor'

content

'#006ef6'

< meta

name

'msapplication-TileImage'

content =' /….png'

< meta

name

'msapplication-config'

content

'/browserconfig.xml'

Slide 97

Slide 97

Everything you know about performance is now wrong and former best practices are now an anti-pattern and considered harmful.

Slide 98

Slide 98

Everything you know about performance is now

twice as complicated.

Slide 99

Slide 99

Thank you Peter Wilson • peterwilson.cc • @pwcc