1 (edited by btcoburn 2005-07-25 11:00)

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.

2

Re: Compressed CSS

A good thing will be removing all /* comments */, trailing tabs and spaces.
You may use something like <link rel="stylesheet" type="text/css" href="style.php?n=style_name" />
And script will read css file, remove all comments from it, and output it compressed (if enabled)
@import url() inside of css files also have to be replaced to .php script.

But other way - is to set apache textfiles compression (including javascripts, and css files).

P.S. removing comments will make styles 1.5 times lighter. Those comments is needed only when you're editing them, not sending to client.

Re: Compressed CSS

I hadn't started editing the CSS yet so I wasn't thinking about comments.... Added another script that is included at the end of the CSS files to strip comments and blank lines. I'm liking this much better now! All the default CSS takes less than 3.3kb to load this way. (That's 17% of the uncompressed size.)

IMHO, gzip transport encoding and comment striping of CSS file should be added in the next release.

While the single script for retrieving CSS files via a GET attribute may look cleaner I think I'll stick with processing all the "CSS files" through PHP. While I'm not planing on using it now, it does give one the opportunity to dynamically generate CSS styling. For example one could use the user-agent string to detect small devices like the zaurus, palm pilot, psp, etc. and produce different CSS formating so the forum would still be legible.

Re: Compressed CSS

Or you could compress the CSS with this online tool

http://www.cssdrive.com/index.php/main/csscompressor/

then paste it over the old css... saves about 50% depending on how you set it.

Re: Compressed CSS

Involving PHP is outputting the style sheets is in my opinion a bad idea. Doing what you suggested above might decrease the bandwidth usage a bit, but it will involve a second PHP "instance" (not quite true with mod_php) for each request. If you want to gzip your stylesheets, use mod_gzip, mod_deflate or whatever suits your needs.

Example: http://leknor.com/code/gziped.php?url=h … Sulfur.css

"Programming is like sex: one mistake and you have to support it for the rest of your life."

Re: Compressed CSS

Most browsers cache external CSS files, so overhead from modifying and compressing the CSS files would not be incurred for each page load. Have a look at Apache's access log while viewing punbb with various browsers. Firefox and IE (depending on user settings) will only request the external CSS files on the first page load.

More importantly, bandwidth is the limiting factor for most web sites. When was the last time you saw a shared web-hosting provider that charged for (or even published a quota on) cpu usage? By the time that cpu usage becomes a real issue, I suspect the web site would be in need of it's own server and rack space in a colocation facility anyway....

7

Re: Compressed CSS

btcoburn, when you're hitting F5 in your browser (any of them), there won't be any using of cached files.
it's a feature of browsers. "Refresh" is refreshing all of files, even if you force them to be cached.

Re: Compressed CSS

i don't think thats strictly true, don't you need to do shift-f5 or something to force the files to be redownloaded? and refresh only refreshes the actual page?

9

Re: Compressed CSS

Any interest to compress CSS.

The best thing is
1. to create a common CSS (body, header, footer ...)
2. for special pages, add into the css (with php) @import url(yourcss.css)

And I'm ok to win Bandwith ... but my BIG css (about 14 ko) ... are loaded 1 time, the first time. Optimize, OK, but loose time with these tools ... no interest (bis)

10 (edited by btcoburn 2005-07-28 15:20)

Re: Compressed CSS

I was not using "refresh" at all when checking the CSS requesting behavior of browsers. I traversed the site the way any normal user would... (paging through a few forums and posts) so the browsers "refresh" behavior is irrelevant.

(Connorhd: Yes you have to specifically request Firefox to "refresh everything" to get external CSS reloaded when "refreshing" a page. I discovered this the hard way some time ago when trying to preview CSS styles I was editing.....)

Rod wrote:

but my BIG css (about 14 ko) ... are loaded 1 time, the first time.

Right, CSS really is not that big, but the forum compresses "everything else" when compression is turned on. It seemed like CSS should be compressed too, if only for consistency.

Re: Compressed CSS

The better way to do this is:

Keep a readable copy of your css in separate .txt files.

Run the production copy of your .css files through:
http://cdburnerxp.se/cssparse/css_optimiser.php