Improving Performance Of Web Sites From a Frontend Point of View
Wednesday, October 15th, 2008This notes are from the book High Performance Web Sites by O’Reilly.
The Firefox plugin YSlow helps you to remind you of them.
- Make fewer HTTP requests
- Use Image Maps instead of many images (for example for a menu)
- Don’t include many JS and CSS files. Ideally just one per page (that means you should try to group your JS and CSS code by page/section, but the modularity is also important)
- Use a content delivery network
- or anyway aim to put the content (geographically speaking) near where your users live.
- Add an Expires Header
- Apache mod_expires
<IfModule mod_expires.c> ExpiresActive on ExpiresByType image/gif "access plus 6 months" ExpiresByType image/png "access plus 6 months" ExpiresByType image/jpeg "access plus 6 months" ExpiresByType text/css "access plus 6 months" ExpiresByType application/x-javascript "access plus 6 months" ExpiresDefault "access plus 1 days" </IfModule>
- If you set an expire date, the only secure way to make sure the browser doesn’t use the copy in its cache (we need this because, for example, we changed the component) is to change the name of the component (or, at list, append a random id as a query string to the filename). A good practise could be to always append a revision number to the filenames.
- You can use an expire date for css and js as well, but bear in mind the previous rule.
- Apache mod_expires
- Gzip components
- The server knows whether it can compress the files it sends depending on the header Accept-Encoding of the request
- Apache2 mod_deflate
AddOutputFilterByType DEFLATE text/html text/css application/x-javascript
-
Trade-off with CPU demanded for compression. Then it’s worth to compress files > 1 or 2 KB. You can use:
mod_gzip_minimum_file_size
- For proxy issues, do use:
Vary: Accept-Encoding,User-Agent
This topic is tricky and it would be worth to spend some time digging around it.
- Put ALL your stylesheets in the HEAD using the LINK tag
- That’s because the browser wants all the stylesheets before rendering the page
- Put Javascript at the bottom of the page
- That’s because the downloading of the script normally block the rendering of the page
- Anyway if your script contains document.write you need to put it in the middle of the HTML code
- Avoid CSS expressions
- Put your JS and CSS in external files
- That’a because, in that way, those components can be cached.
- Then it’s not very beneficial to use this rule, if those components of yours are not cached. It would be faster to use inline JS and CSS.
- You can use Post-Onload Download. You use the onLoad JS event so you are sure the page is already loaded. Anyway it’s better to wait 1 more second just to be even more sure(for this purpose you can use setTimeOut function). Then you start to download CSS and JS files you are going to use in other pages. You do that using the DOM structure of the document (you use appendChild to the document)
- Reduce DNS Lookups
- Make sure you use Keep-Alive (see httpd.conf)
- In your HTML document, use as fewer domain names as possible
- Minify Javascript
- Avoid Redirects
- They are bad because for a while there is not content at all in the browser
- Most common are: 301 (moved permanently) and 302 (moved temporary)
- many way to perform redirect (meta refresh, Javascript) but through HTTP header is the best performance-wise.
- To avoid redirects you can use mod_rewrite, Aliases, some internal PHP logic.
- Remove duplicate scripts
- It’s bad to have them because it means one more request (not necessary, depends on the browser) and more script evaluation
- It happens when many people work on the same project
- Solution: use PHP to add JS files to a page (it will do checks before adding)
- Configure/Remove ETags
- If the application run on just one server you can forget about them
- They are used in conditional gets and, on cluster architectures, make components less cachable
- In Apache, to remove them, add this in the configuration file
FileETag none
- Make Ajax cacheable
- There are two types of Ajax requests: active (in response of a user’s action) and passive (the most common example is preloading of content and images)
- You can make Ajax cacheable telling the browser to do so! So you need to set Cache-Control and Expire HTTP headers.

