Blog Optimization – Getting Started with Mod Concat

Following my post on Blog Bloat, I have finally gotten around to installing mod_concat. I asked my hosting provider ServInt to handle the upgrade to Apache 2.2.x, they did it in about 20 mins – thanks guys, you rock!

The actual installation of mod_concat was fairly simple thanks to the included instructions, though it lacked a few of the finer points, like needing to install the module into the Apache modules directory and modifying httpd.conf to include mod_concat, this is easily handled by adding the -i and -a flag. First grab the project out of SVN, then you can compile it.

prompt:$ svn checkout http://modconcat.googlecode.com/svn/trunk/ modconcat-read-only
prompt:$ cd modconcat-read-only/mod_concat
prompt:$ apxs -i -a -c mod_concat.c 
#Make sure the config file is working okay
prompt:$ apachectl configtest
#Then it's safe to restart and use the concat module
prompt:$ apachectl -k graceful

That’s all it takes to get the module working with Apache 2.2.x. I setup a super simple test here to ensure mod_concat is working.

Optimizing WordPress K2 using mod_concat

Getting this Apache module working with WordPress and K2 wasn’t quite as simple as I was hoping. I ended up having to do more customization work than I desired. The issue with K2 is that many of the included JavaScript files, for handling the rolling archives and live search, have a .php extension that includes a gzip directive header. Additionally, the K2 files are nested so deep that mod_concat can’t handle it.

To deal with these short comings, I sourced the K2 JavaScript files and moved them to a root directory. Currently, the files to be concatenated are hard coded into the plugin. Perhaps a future revision will resolve this, however, I am more interested in testing out mod_jsmin and mod_deflate before I get around to this enhancement.

Here is the plugin code I wrote in order to use mod_concat with K2.

<?php
/*
Plugin Name: WP Mod Concat
Description: This plugin leverage the Apache 2.2.x module mod_concat to combine a list of files into a single file
Version: 0.1
Author: Gregory Tomlinson
Author URI: http://gregorytomlinson.com/encoded/
*/
 
remove_action('wp_head', 'wp_print_scripts');
add_action( 'wp_head', 'wp_mod_concat_optimzie' );
 
function wp_mod_concat_optimzie(  $handles = false  ) {
        $base_url = "http://gregorytomlinson.com/js/??";
        $script_files = array("jquery_1_2_6.js", "k2.functions.js", "k2.livesearch.js",  "k2.rollingarchives.js",  "k2.slider.js", "k2.trimmer.js", "swfobject.js" );
 
        echo '<script type="text/javascript" src="'. $base_url . join(",", $script_files) .  '"></script>';
}
?>

By using remove_action(’wp_head’, ‘wp_print_scripts’);, I can stop WordPress from loading JavaScript files. I then replace the head script files with the concatenated version leveraging mod_concat, which produces a script tag that looks like the following.

<script type="text/javascript" src="http://gregorytomlinson.com/js/??jquery_1_2_6.js,k2.functions.js,k2.livesearch.js,k2.rollingarchives.js,k2.slider.js,k2.trimmer.js,swfobject.js"></script>

You can learn more about mod_concat here.

The other drawback to mod_concat is it’s incapable of handling query parameters. I would like to be able to append a ?ver=1.1 in order to handle busting the browser cache, but the current version isn’t able to do this, maybe Ian can fix this :)

That’s all it takes to use mod_concat. It’s probably not the ideal method to use with WordPress and K2, you may consider using PHP Speedy. Dave Artz runs down a list of other concatenation methods in this blog post.

  • I've been using PHP Speedy which works quite well. I might give yours a whirls
  • Thanks Gregory.
    i've updated the documentation to reflect the apxs -i -a -c option.

    as for the '?ver' parameter. what would you like it to do ? at the moment the code just reads files directly. so the query args would be ignored.

    I think what you would need is for concat to be changed so that it actually can run php code and merge the result. but this won't help the 'gzip' problem your having.

    as for recursion, if you could raise an issue/bug with that problem and an example I'll try to reproduce and fix it.

    Regards
  • Thanks Ian, I am working with K2 team to change the .php to static files (they aren't dynamic). Though having the module execute PHP could be very useful.

    for the ?ver=1.1, I was thinking it would help to bust browser cache by changing the file name, not for any dynamic reasons. though from my recent tests, this hasn't been an issue. If I had an art origin server and CDN, this would help to break the akamai cache.

    I have reported an issue in the google code here
    http://code.google.com/p/modconcat/issues/detai...

    thanks!
blog comments powered by Disqus