Lee Willis

Gzip in Joomla – Tips for a faster website

| 9 Comments

The average internet connection is much more capable of delivering sizeable web content today than it was even 12 months ago – the UK average broadband connection at time of writing is 4.6Mb/s.

The problem

It’s important to remember that:

  1. Not everyone has broadband
  2. Most web browsers don’t take advantage of the bandwidth available

GZIP Compression

If you’re developing a site in Joomla for example you often hear people suggesting “turn on compression” within Joomla. This option compresses the page content created by Joomla before sending it back to the browser in real-time.

While this is a great solution for some sites, there are significant issues with this approach for many sites. It’s important to realise that the HTML page is only 1 of the many requests needed to generate a webpage – you also need all of the images, CSS files, and JS.

Take for example a typical template from YOOtheme – a major Joomla theme provider. The actual HTML for March 2009 theme demo is 1 out of 31 requests, and just 7kB out of a total of 144 kB.

It’s worth noting that if the objective is to make your site “faster” then Joomla’s gzip may not be the best option. The downsides to this approach are:

  • It only affects the size of the HTML page
  • It does that by compressing the content on-fly
    • Cacheing aside – this can result in you compressing the file every time it is requested – adding a significant processor load to your server
    • The process of compressing the data adds a delay (However small) before your content can be sent
  • It does nothing about the other files that make up the webpage
  • It doesn’t maximise your bandwidth usage by minimising total number of requests

My Approach

For the sites that I run I use the following approach:

  • Identify files that are included widely, but updated infrequently
  • Consolidate multiple files into one (ie, create just one .js file for your site, and one .css file)
  • Create a compressed version of these files alongside the original
  • Use a bit of .htaccess magic to serve the compressed files to everyone who can deal with them.

This is a good solution for files which don’t change frequently (CSS, template images, JS includes etc.) This approach gives you the best of both worlds, compressed files, but low server CPU impact.

The HOWTO

To implement it simply create a compressed copy of the file(s) you want to compress. If you have shell access to your website files you can do:

$ gzip -9 -c foo.js > foo.jsgz

This should leave you with your original uncompressed file, and a compressed variant with the suffix “gz”.

Now add the following to your .htaccess file:

#Check to see if browser can accept gzip files.
ReWriteCond %{HTTP:accept-encoding} (gzip.*)
#make sure there’s no trailing .gz on the url
ReWriteCond %{REQUEST_FILENAME} !^.+gz$
#check to see if a .gz version of the file exists.
RewriteCond %{REQUEST_FILENAME}gz -f
#All conditions met so add .gz to URL filename (invisibly)
RewriteRule ^(.+) $1gz [L]
AddType “text/css;charset=UTF-8” .cssgz
AddEncoding gzip .cssgz
AddType “text/javascript;charset=UTF-8” .jsgz
AddEncoding gzip .jsgz

Remember if you do change any of the uncompressed versions you’ll have to re-generate the compressed version otherwise clients that support gzip won’t see the changes.

There are some complications when using Joomla with Virtuemart (Since it insists on loading CSS/JS support files itself) – but that’s a topic for another day!

When we implemented this on SnugBaby it reduced the total number of files required from over 35 to 27, and the total download size from over 200k to 144k – would love to hear other’s experiences.

9 Comments

  1. Nice article about gzipping on snugbaby. You’ve got 7 scripts and 6 stylesheet perhaps these could these could be combined n crushed also etags & headers – would improve the grade in yslow (which would benefit snugbaby – if the server is caching the asset files once combined)

    The if joomla has a supercache type plugin also cache the output of the page. This way you can get the loading times of static html site with a fully content managed dynamic site

  2. You’re right, there are a bunch of scripts. Unfortunately these are all added by a host of different components / modules and I haven’t taken the time to go through them all and merge them. I have merged /removed some (There were about 12 scripts and about 8 stylesheets).

    I’ve also added Expires headers on the various resources images/css (I’ve been meaning to do that for ages, but it just kept dropping to the bottom of the list!).

    I’ve never been a big fan of on the fly compressing the HTML (I’ve trying to avoid as much processing as possible) although Joomla does have page cache functionality.

    Thanks for the comments.

  3. I implemented this for my CSS files only on my site. I merged all the CSS files in the header to template.css.

    I think it’s working. How do I know if it is? If it is working, it’s reducing the size of my main template.css from 50 KB to about 10 KB.

    Site is under my signature above.

    There’s a PHP include above that is loading a number of other CSS files for the template. These come into play as the template changes color depending on the section being read.

    Thanks for the tips!

  4. To check – you can install the excellent Firebug extension for firefox – https://addons.mozilla.org/en-US/firefox/addon/1843.

    This will show you all the requests that are being made, and the download sizes. It will also show you that the content encoding used (You’re looking for gzip).

    It doesn’t look like this is happening on your site. I still get the uncompressed 44k version. Have you made the .htaccess changes?

    Can you add a line that says
    RewriteEngine On
    before the chunk I mentioned earlier and see if that helps?

  5. I added the line just before your code in the .htaccess file. The line was already in the file. Perhaps this makes a difference.

    I installed Firebug but can’t Firebug but can’t figure out how to read the CSS I’m downloading.

  6. I implemented this for my CSS files only on my site. I merged all the CSS files in the header to template.css.

    I think it’s working. How do I know if it is? If it is working, it’s reducing the size of my main template.css from 50 KB to about 10 KB.

    There’s a PHP include above that is loading a number of other CSS files for the template. These come into play as the template changes color depending on the section being read.

    Thanks for the tips!

  7. Remember if you do change any of the uncompressed versions you’ll have to re-generate the compressed version otherwise clients that support gzip won’t see the changes.

    I am not very clear on this part

  8. Hi,
    I’ve compressed some css files and added your code to .htaccess, works great from chrome, css are not being loaded in Firefox and some android browsers, what could I making wrong ?

    Best Regards,
    Fernando

Leave a Reply

Required fields are marked *.