Skip to content

Commit

Permalink
MDL-32683 fine tune theme style sheet caching
Browse files Browse the repository at this point in the history
  • Loading branch information
skodak committed May 5, 2012
1 parent 8475b97 commit 2c61e7c
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 15 deletions.
30 changes: 18 additions & 12 deletions lib/csslib.php
Original file line number Diff line number Diff line change
Expand Up @@ -85,23 +85,28 @@ function css_store_css(theme_config $theme, $csspath, array $cssfiles) {
*
* @param string $themename The name of the theme we are sending CSS for.
* @param string $rev The revision to ensure we utilise the cache.
* @param string $etag The revision to ensure we utilise the cache.
* @param bool $slasharguments
*/
function css_send_ie_css($themename, $rev, $slasharguments) {
$lifetime = 60*60*24*30; // 30 days
function css_send_ie_css($themename, $rev, $etag, $slasharguments) {
global $CFG;

$lifetime = 60*60*24*60; // 60 days only - the revision may get incremented quite often

$relroot = preg_replace('|^http.?://[^/]+|', '', $CFG->wwwroot);

$css = "/** Unfortunately IE6/7 does not support more than 4096 selectors in one CSS file, which means we have to use some ugly hacks :-( **/";
if ($slasharguments) {
$css .= "\n@import url(styles.php/$themename/$rev/plugins);";
$css .= "\n@import url(styles.php/$themename/$rev/parents);";
$css .= "\n@import url(styles.php/$themename/$rev/theme);";
$css .= "\n@import url($relroot/styles.php/$themename/$rev/plugins);";
$css .= "\n@import url($relroot/styles.php/$themename/$rev/parents);";
$css .= "\n@import url($relroot/styles.php/$themename/$rev/theme);";
} else {
$css .= "\n@import url(styles.php?theme=$themename&rev=$rev&type=plugins);";
$css .= "\n@import url(styles.php?theme=$themename&rev=$rev&type=parents);";
$css .= "\n@import url(styles.php?theme=$themename&rev=$rev&type=theme);";
$css .= "\n@import url($relroot/styles.php?theme=$themename&rev=$rev&type=plugins);";
$css .= "\n@import url($relroot/styles.php?theme=$themename&rev=$rev&type=parents);";
$css .= "\n@import url($relroot/styles.php?theme=$themename&rev=$rev&type=theme);";
}

header('Etag: '.md5($rev));
header('Etag: '.$etag);
header('Content-Disposition: inline; filename="styles.php"');
header('Last-Modified: '. gmdate('D, d M Y H:i:s', time()) .' GMT');
header('Expires: '. gmdate('D, d M Y H:i:s', time() + $lifetime) .' GMT');
Expand All @@ -122,11 +127,12 @@ function css_send_ie_css($themename, $rev, $slasharguments) {
* request, then optimised/minified, and finally cached for serving.
*
* @param string $csspath The path to the CSS file we want to serve.
* @param string $rev The revision to make sure we utilise any caches.
* @param string $etag The revision to make sure we utilise any caches.
*/
function css_send_cached_css($csspath, $rev) {
$lifetime = 60*60*24*30; // 30 days
function css_send_cached_css($csspath, $etag) {
$lifetime = 60*60*24*60; // 60 days only - the revision may get incremented quite often

header('Etag: '.$etag);
header('Content-Disposition: inline; filename="styles.php"');
header('Last-Modified: '. gmdate('D, d M Y H:i:s', filemtime($csspath)) .' GMT');
header('Expires: '. gmdate('D, d M Y H:i:s', time() + $lifetime) .' GMT');
Expand Down
11 changes: 8 additions & 3 deletions theme/styles.php
Original file line number Diff line number Diff line change
Expand Up @@ -65,10 +65,11 @@
}

if ($type === 'ie') {
css_send_ie_css($themename, $rev, !empty($slashargument));
css_send_ie_css($themename, $rev, $etag, !empty($slashargument));
}

$candidatesheet = "$CFG->cachedir/theme/$themename/css/$type.css";
$etag = sha1("$themename/$rev/$type");

if (file_exists($candidatesheet)) {
if (!empty($_SERVER['HTTP_IF_NONE_MATCH']) || !empty($_SERVER['HTTP_IF_MODIFIED_SINCE'])) {
Expand All @@ -79,9 +80,10 @@
header('Expires: '. gmdate('D, d M Y H:i:s', time() + $lifetime) .' GMT');
header('Cache-Control: public, max-age='.$lifetime);
header('Content-Type: text/css; charset=utf-8');
header('Etag: '.$etag);
die;
}
css_send_cached_css($candidatesheet, $rev);
css_send_cached_css($candidatesheet, $etag);
}

//=================================================================================
Expand All @@ -95,6 +97,9 @@

$theme = theme_config::load($themename);

$rev = theme_get_revision();
$etag = sha1("$themename/$rev/$type");

if ($type === 'editor') {
$cssfiles = $theme->editor_css_files();
css_store_css($theme, $candidatesheet, $cssfiles);
Expand All @@ -119,4 +124,4 @@
$cssfile = "$CFG->cachedir/theme/$themename/css/all.css";
css_store_css($theme, $cssfile, $allfiles);
}
css_send_cached_css($candidatesheet, $rev);
css_send_cached_css($candidatesheet, $etag);

0 comments on commit 2c61e7c

Please sign in to comment.