Using YSlow to optimize your website02 Jul 2009
In the last post I've introduced simple techniques to improve the performance of a web application. In this post I will talk about performance optimization on the browser side. With a few easy steps, the pages will load a lot faster and you will reduce bandwidth consumption and load on your servers.
Yahoo! has released a great tool called YSlow. YSlow analyzes web pages and suggests ways to improve their performance based on a set of rules for high performance web pages. It is an addon for Firebug, which in turn is an add-on for Firefox.
There are 22 rules defined in YSlow:
- Minimize HTTP Requests
- Use a Content Delivery Network
- Add an Expires or a Cache-Control Header
- Gzip Components
- Put StyleSheets at the Top
- Put Scripts at the Bottom
- Avoid CSS Expressions
- Reduce DNS Lookups
- Avoid Redirects
- Remove Duplicate Scripts
- Configure ETags
- Make AJAX Cacheable
- Use GET for AJAX Requests
- Reduce the Number of DOM Elements
- No 404s
- Reduce Cookie Size
- Use Cookie-Free Domains for Components
- Avoid Filters
- Do Not Scale Images in HTML
- Make favicon.ico Small and Cacheable
Since I like to learn by example, I've created a simple, rather ugly web application called Cootweets. It will be some sort of a "geo tagged flickr image with a tweet attached to it"-thing. So throughout this blog post, I will try to optimize the start page of Cootweets to satisfy YSlow's requirements. I won't talk about every rule, just the ones that YSlow complained about. Here is what the start page looks like: :
/public/images/posts/="/public/images/posts/cootweets_home_post_format.jpg" alt="cootweets_home_post_format" title="cootweets_home_post_format" width="389" height="362"/>
So lets get started and make the start page load faster.
Make fewer HTTP requests (B)
What is this about?
Lets fix it
Add Expires headers (F)
What is this about?
Expires headers basically tell the browser how long it can cache a specific component. That avoids unnecessary HTTP requests on subsequent page views. That speeds up page load, saves bandwidth and reduces the load on the servers. If you use a CDN, setting the Expires headers will save you money.
Lets fix it
Cootweets runs on Nginx and Passenger. Here is how I changed the Nginx configuration:
If you are running Apache, the solution is similar, but somehow I could not convince Apache only to cache assets that have a time stamp appended. I tried the same regular expression I used with Nginx, but then Apache just didn't set any Expires headers at all. This actually makes sense, because the directive is called FilesMatch and not RequestMatch. I have come up with a workaround using Apache variables.
However, this does not set the time dynamically. Well of course, we could alter the Apache configuration on each deploy to fix this but that seems cumbersome for such a little task. If someone knows how to do this dynamically with an Apache directive, please let me know.
You can check if it works by looking at the HTTP headers returned from the webserver. YSlow offers a convenient way to do this but you can also use curl with the --head option to look at the headers.
/public/images/posts/="/public/images/posts/expire_headers_post_format.jpg" alt="expire_headers_post_format" title="expire_headers_post_format" width="389" height="212"/>
Looking at the browser logs, I have noticed something strange. Even though the Expires headers are set correctly, Safari and Firefox still requested the components at the server and got back a 304 Not modified. While this is an improvement, I prefer no requests over a few requests. It took me quiet some time to figure out that this only happens when an explicit reload is triggered in the browser.
Compress components with GZip (D)
An here for Apache:
/public/images/posts/="/public/images/posts/cache_statistics-300x124.jpg" alt="cache_statistics" title="cache statistics" width="300" height="124" class="alignnone size-medium wp-image-104" />
YSlow is a very useful tool to quickly analyze the page load performance. It offers a rich set of information and tools comfortably integrated in Firefox. Google has recently released a similar tool called Page Speed. The cool thing about page speed is that it actually estimates the bandwitdh reduction that could be achieved, so make sure to check it out too.