Asset Redux - Front end performance on Rails (Phil Nash)

Post on 21-Jan-2015

145 views 4 download

Tags:

description

Slides from Phil Nash's presentation at the London Web Meet-up - http://londonweb.org Speaker Phil Nash is a developer evangelist for Twilio and a Ruby and JavaScript developer. He loves test coverage, great beer, hackathons, and gems with puns in their names. Get all four together for maximum points. He once made a pull request to Rails... it's still open ;) Overview of session: Web application speed is paramount. Our users want our application and they want it now! We can optimise application code, database queries and so on, but that's all wasted if the page takes ages to appear. A fast back end and a slow front end can end up leaving a bad taste in the mouth. Using Rails, we'll look at the best ways to speed up the delivery of your application. Going beyond just minifying our assets, we'll look at techniques to get our site in the user's browser quicker, improving both real and perceived speed. We'll also discover the best tools to use to check out speed and get a better idea of the user's opinion of the site.Once finished, our sites will load in a flash!

transcript

ASSET REDUXLondon Web — August 2014

Phil Nash@philnash

philnash@twilio.com

PERFORMANCE &RAILS

FRONT ENDPERFORMANCE

Why front end performance?

•  Sites are getting bigger•  More people are going mobile•  I'm a busy person

Top 1000 sites

Month/Year Average page size Average number of requests

June/2011 690kB 85

June/2014 1492kB 108

Source: http archive

Why Rails?

•  It's what I know

Why Rails?

But seriously...

WHY AM I TALKINGABOUT THIS?

PERFORMANT BYDEFAULT

TOOLS

Tools

•   WebPageTest•   Google PageSpeed Insights•   YSlow•   Real User Monitoring

The problems

•  Bandwidth•  Latency•  User attention

Delay User perception

0-100ms Instant

100-300ms Small perceptible delay

300-1000ms Machine is working

1000+ms Likely mental context switch

10000+ms Task is abandoned

Source: High performance browser networking

FAST FRONT ENDS

WARNING!

TEXT ASSETS

CONCATENATE ANDMINIFY

Thanks asset pipeline!

CHOOSE WHAT TOCONCATENATE

GZIPThanks asset pipeline!

I <3 ASSETPIPELINE

nginx

location ~ ̂/(assets)/ {

root /path/to/public;

gzip_static on; # to serve pre-gzipped version

expires max;

add_header Cache-Control public;

}

Source: RailsGuides

01.

02.

03.

04.

05.

06.

Heroku

•  heroku_rails_deflate•  rack-zippy

CACHE HEADERS

nginx

location ~ ̂/(assets)/ {

root /path/to/public;

gzip_static on;

expires max;

add_header Cache-Control public;

}

01.

02.

03.

04.

05.

06.

Heroku

MyApp::Application.configure do

config.serve_static_assets = true

config.static_cache_control = "public, max-age=31536000"

end

01.

02.

03.

04.

THE DOM AND THECSSOM

CSS IN THE HEADJS AT THE BOTTOM

TURBOLINKS

TURBOLINKS

CSS and the critical path

Inline the CSS required for above the fold in the headLoad the rest at the bottom

Patrick Hamann on CSS and the critical path

The Guardian team load CSS from localStorage

Critical path CSS generator

IMAGES

LOSSLESSCOMPRESSION

Assets

Get the asset pipeline to do it!

sprockets-image_compressor

Uploads

Plugins!

paperclip-optimizer

carrierwave-imageoptimizer

PROGRESSIVEJPEGS

Source: Book of speed

Assets

Get the asset pipeline to do it!

image_optim

Paperclip

has_attached_file :image, {

:styles => {

:progressive => '1x1<'

},

:convert_options => {

:progressive => '-interlace Plane'

}

}

01.

02.

03.

04.

05.

06.

07.

08.

INLINING

Inlining

rails-sass-images

.mail a {

background-image: inline("svg/mail.svg");

}

01.

02.

03.

HTTP 2.0

SPDY

HTTP 2.0/SPDY Features

•  Single connection - multiplexed streams•  Request prioritization•  Server push

Fixing for SPDY

•  Stop concatenating/inlining

THANKS

Questions?@philnash

philnash@twilio.com