improved stylesheet cacheing

improved stylesheet cacheing

by John Dell -
Number of replies: 2
With nothing better to do on a pleasant Sunday...I noticed that stylesheets are cached for 5 minutes using style_sheet_setup () in weblib.php.

This is good, but I think it can be better (emulate behavior of files with css extension). Normal stylesheets with css extension are automatically checked against request header 'last-modified' date by the webserver for modify and if not return the '304 Not Modified' header saving valuable bandwidth and performance.

However, a file with a php extension won't do this without mucking with the headers. I love the added flexibility of using the php file extension for stylesheets as evidenced by what moodle does in its stylesheets. Certainly makes it much more manageable.

I recently did some work on css cacheing for the seagull app framework with php file extensions and got it working by adapting (and fixing smile the code in PEAR.

I think this approach can be used for moodle as well. Basically, the client gets the last-modified timestamp the first time in the header, and on subsequent requests, the styles.php file checks the request header against it's current modified datetime and either sends a new file or sends the '304 Not Modified' header as appropriate.

This could easily be incorporated into the current styles with about 2 lines of code and and an include file (see attached) for each style.

You can validate that it is working with one of the coolest tools I've found in a long time called livehttpdheaders at mozdev (for mozilla). http://livehttpheaders.mozdev.org/

Regards,
John

Here is a sample stylesheet.php use of attached file:

<?php

require_once "css-cache.inc.php";
exitIfCached(getlastmod());

// CSS Substitution Variables
$primary = '#99CC00';

?>

/* stylesheet contents */
body {
margin: 0;
padding: 0;
font-family: "verdana";
font-size: 0.8em;
color: <?php echo "$primary"?>;
}

Average of ratings: -
In reply to John Dell

Re: improved stylesheet cacheing

by Martin Dougiamas -
Picture of Core developers Picture of Documentation writers Picture of Moodle HQ Picture of Particularly helpful Moodlers Picture of Plugin developers Picture of Testers
Good call, John! Thanks! approve

I added the relevant parts of that include to the style_sheet_setup function in CVS so that it will now return a 304 as appropriate without needing to change any style sheets.
In reply to Martin Dougiamas

Re: improved stylesheet cacheing

by John Dell -
Nice!  Just tested with latest cvs and it is working great for me!

Regards,
John