Topic: Compressed CSS
I've been modifying a new installation of the 1.2.6 code and noticed that the CSS files are not sent compressed. Given that the compressed html is only a few kb, this adds a lot of overhead to the first page request. So, I renamed the CSS files to *.php and added code to send them gzip compressed if the board is configured for that. The result was that the default theme was reduced to 34% of it's size, saving ~12kb per CSS request.
This seems like a good thing. It would be nice to see this code (or something with the same effect) included in punbb.
Here's the code I used. The changes to rest of punbb were to account for renaming *.css to *.php and to hide compress_css.php in the theme selection menus.
<?php
// style/compress_css.php
// Usage: include this at the top of each *.css file that has been renamed *.php
// Requires edits to all the *.css files, css include statements, html generation, and punbb style admin.
// Also edit: header.php, admin_options.php, profile.php, include/functions.php, (and install.php)
// gzip encoding of content (from punbb: /include/common.php)
// tweeked for CSS (see: http://www.fiftyfoureleven.com/sandbox/weblog/2004/feb/gzipping-your-css-with-php/ )
$css_cache_time_in_seconds = 86400; // 3600=1hour, 86400=1day
// fixes include path relative to compress_css.php (not the http requested file)
@include realpath(dirname(__FILE__).'/'.'../cache/cache_config.php');
$enable_css_compression = (defined('PUN_CONFIG_LOADED') && $pun_config['o_gzip']==1)?true:false;
// For some very odd reason, "Norton Internet Security" unsets this
$_SERVER['HTTP_ACCEPT_ENCODING'] = isset($_SERVER['HTTP_ACCEPT_ENCODING']) ? $_SERVER['HTTP_ACCEPT_ENCODING'] : '';
// Should we use gzip output compression?
if ($enable_css_compression && extension_loaded('zlib') &&
(strpos($_SERVER['HTTP_ACCEPT_ENCODING'], 'gzip') !== false ||
strpos($_SERVER['HTTP_ACCEPT_ENCODING'], 'deflate') !== false))
{ ob_start('ob_gzhandler'); }
// CSS headers
header("Content-type: text/css");
header("Cache-Control: must-revalidate");
$ExpStr = "Expires: ".gmdate("D, d M Y H:i:s", time() + $css_cache_time_in_seconds)." GMT";
header($ExpStr);
?>
Aside from renaming the CSS files and the changes mentioned above, all that is left to be done is add "<?php require 'compress_css.php'; ?>" or "<?php require '../compress_css.php'; ?>" to the front of each CSS file (depending on it's path).
Also note that this code acknowledges the boards preferences for gzip content compression.