Front-End Performance Optimization in WordPress

31
[email protected] front-end performance optimization #WCDAYTON

description

Data from major Internet providers like Google, Amazon and Akamai has shown that how fast a website loads significantly affects user behavior. And because users don’t like slow sites, Google uses load time as a factor in computing PageRank results. In short: It pays to be fast. There are a lot of factors that can affect your site’s performance. While some are dependent on your hosting environment, there are plenty of factors beyond server/internet speed (and the obvious sheer number of bits to be loaded) that affect your page load time, such as HTTP connections, DNS lookups, and asset load sequencing. If you’re a front-end developer and you’re serious about building websites that load as fast as possible, come learn about techniques (such as non-blocking Javascript) you can use in your markup and themes — whether on WordPress or some other system — to help things load as quickly as possible. We’ll also review tools you can use to assess whether your site is doing all it can to load quickly.

Transcript of Front-End Performance Optimization in WordPress

Page 1: Front-End Performance Optimization in WordPress

[email protected]

front-end performance optimization

#WCDAYTON

Page 2: Front-End Performance Optimization in WordPress

SPEED MATTERSSEO - UX - Sales - Mobile

Page 3: Front-End Performance Optimization in WordPress

OUR ENEMIES

Payload size Bloated DOM & CSS HTTP connections JavaScript blocking

DNS lookups

Page 4: Front-End Performance Optimization in WordPress

NOT COVERED

Apache/mySQL config !

Using a CDN !

Choosing a good host

Page 5: Front-End Performance Optimization in WordPress

REDUCE PAYLOADa.k.a. front-end performance 101

Page 6: Front-End Performance Optimization in WordPress

REDUCE ASSET SIZES

Minify your JS and CSS (and HTML)

Load properly-sized images

Use right image filetypes (gif/jpg/png/svg)

Page 7: Front-End Performance Optimization in WordPress

DEFER ASSET LOADING

“lazy load” images when they appear in browser window

Use async/defer <script> attributes

!

Also, load pre-cached stuff!

<script defer async src="script.js"></script>

Page 8: Front-End Performance Optimization in WordPress

TRICKING WP_ENQUEUE_SCRIPTfunction  add_async(  $url  )  {      if  (strpos($url,  '#async')===false)          return  $url;      else  if  (is_admin())          return  str_replace('#async',  '',  $url);      else          return  str_replace('#async',  '',  $url)."'  async";    }  add_filter('clean_url',  'add_async',  11,  1);

wp_enqueue_script('demo',  '/js/myscript.js#async'  );

Page 9: Front-End Performance Optimization in WordPress

USING GOOGLE’S JQUERY

wp_deregister_script('jquery');  wp_register_script(      'jquery',        'http://ajax.googleapis.com/ajax/libs/jquery/1.11.0/jquery.min.js',      array(),      '1.11.0'  );  

Page 10: Front-End Performance Optimization in WordPress

BLOATED DOM & CSSmake it clean before you make it mini

Page 11: Front-End Performance Optimization in WordPress

MORE ELEMENTS = SLOWER<body class="page"> <div id="wrapper"> <div id="page"> <div id="main"> <div class="main-side"> <aside id="sidebar"> ... </aside> </div> </div> </div> </div></body>

You can do a count with: !$(‘*’).length; !or !document. getElementsByTagName(‘*”). length

Page 12: Front-End Performance Optimization in WordPress

SIMPLE SELECTORS

html body div#main article#post-22 p a.inline { property: value; }

.inline { property: value; }

VS.

ul li {} is slower than ul > li {} which is slower than .ul-li {}

Page 13: Front-End Performance Optimization in WordPress

HTTP CONNECTIONSyour biggest obstacles to fast loads

Page 14: Front-End Performance Optimization in WordPress

HTTP CONNECTIONS

Each asset (script, image, css file, font, etc) is retrieved via an HTTP

connection.

Each connection takes a moment to start due to overhead.

Page 15: Front-End Performance Optimization in WordPress

HTTP HEADERS

Page 16: Front-End Performance Optimization in WordPress

REDUCING CONNECTIONS

Combine CSS Files

Combine JS Files

Use CSS Sprites

Avoid images in favor of CSS

Don’t load stuff you don’t need*

Page 17: Front-End Performance Optimization in WordPress

COMBINING CSS FILES

Use a tool like SASS that combines them for you

Only write a single style.css

Use a plugin (e.g. W3 Total Cache) to combine (& compress!) them for you.

Page 18: Front-End Performance Optimization in WordPress

COMBINING JS FILES

Use a plugin (e.g. W3 Total Cache) to combine+compress

them for you.

Manually put all your jQuery plugins into a single file.

Page 19: Front-End Performance Optimization in WordPress

COMBINING JS FILES

WordPress is especially prone to loading lots of JS files

It’s worth the effort to mitigate against this. Seriously.

Page 20: Front-End Performance Optimization in WordPress

CSS SPRITES

Put all your images into a single file, and use CSS to position the

background properly.

Page 21: Front-End Performance Optimization in WordPress

CSS SPRITE EXAMPLE

.sprite-ben { height: 117px; width: 91px; background-image: url('img/sprite.png'); background-position: 0 -525px; background-repeat: no-repeat; }

overall sprite.png file measures 304 x 910

but my headshot is a small part

Page 22: Front-End Performance Optimization in WordPress

DON’T USE IMAGES

CSS3 provides alternatives:

Gradients

Rounded Corners

Text and box shadows

Rotation

Page 23: Front-End Performance Optimization in WordPress

JAVASCRIPT BLOCKINGAnd sequential loading general

Page 24: Front-End Performance Optimization in WordPress

SEQUENTIAL VS. PARALLEL

Browsers can load some assets in parallel, such as CSS files, images, and fonts. This is good.

But some assets —JS files — are loaded in sequence and block others.

Page 25: Front-End Performance Optimization in WordPress

CSS AND SCRIPTS

JS should be at bottom of page.

CSS should go at the top of your page and be loaded via

<link> not @import

Page 26: Front-End Performance Optimization in WordPress

IN WORDPRESS

wp_enqueue_script( $handle, $src, $deps, $ver, $in_footer );

Set to TRUE

Page 27: Front-End Performance Optimization in WordPress

DNS LOOKUPShidden time thieves

Page 28: Front-End Performance Optimization in WordPress

DNS LOOKUPS

Every domain mentioned on your page needs to be

resolved to an IP (20-120 ms) !

But too few domains is bad too.

Page 29: Front-End Performance Optimization in WordPress

TOOLS

Page 30: Front-End Performance Optimization in WordPress

FOR MORE

Google “Steve Souder”

https://developers.google.com/speed/docs/best-practices/rules_intro

!http://developer.yahoo.com/yslow/

!https://developers.google.com/speed/

pagespeed/

Page 31: Front-End Performance Optimization in WordPress

CONNECTBen Byrne

[email protected]

facebook.com/drywall

Twitter: @drywall