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:
- Not everyone has broadband
- 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:
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:
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.
May 27, 2009 at 11:21 pm
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
June 1, 2009 at 8:34 pm
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.
June 23, 2009 at 9:35 pm
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!
June 23, 2009 at 10:08 pm
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?
June 23, 2009 at 11:15 pm
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.
June 2, 2010 at 11:38 pm
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!
June 4, 2010 at 10:45 pm
Hi – firebug (http://getfirebug.com) will tell you if the content is being transmitted gzipped or not.
September 8, 2012 at 4:58 am
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
August 16, 2013 at 3:44 pm
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