Skip to content

Commit

Permalink
MDL-32683 fine tune theme javascript caching
Browse files Browse the repository at this point in the history
  • Loading branch information
skodak committed May 5, 2012
1 parent 71d4c60 commit 8475b97
Showing 1 changed file with 11 additions and 5 deletions.
16 changes: 11 additions & 5 deletions theme/javascript.php
Original file line number Diff line number Diff line change
Expand Up @@ -64,19 +64,21 @@
}

$candidate = "$CFG->cachedir/theme/$themename/javascript_$type.js";
$etag = sha1("$themename/$rev/$type");

if ($rev > -1 and file_exists($candidate)) {
if (!empty($_SERVER['HTTP_IF_NONE_MATCH']) || !empty($_SERVER['HTTP_IF_MODIFIED_SINCE'])) {
// we do not actually need to verify the etag value because our files
// never change in cache because we increment the rev parameter
$lifetime = 60*60*24*30; // 30 days
$lifetime = 60*60*24*60; // 60 days only - the revision may get incremented quite often
header('HTTP/1.1 304 Not Modified');
header('Expires: '. gmdate('D, d M Y H:i:s', time() + $lifetime) .' GMT');
header('Cache-Control: public, max-age='.$lifetime);
header('Content-Type: application/javascript; charset=utf-8');
header('Etag: '.$etag);
die;
}
send_cached_js($candidate, $rev);
send_cached_js($candidate, $etag);
}

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

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

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

if ($rev > -1) {
// note: cache reset might have purged our cache dir structure,
// make sure we do not use stale file stat cache in the next check_dir_exists()
Expand All @@ -101,7 +106,7 @@
$fp = fopen($candidate, 'w');
fwrite($fp, minify($theme->javascript_files($type)));
fclose($fp);
send_cached_js($candidate);
send_cached_js($candidate, $etag);
} else {
send_uncached_js($theme->javascript_content($type));
}
Expand All @@ -111,12 +116,13 @@
// we are not using filelib because we need to fine tune all header
// parameters to get the best performance.

function send_cached_js($jspath) {
function send_cached_js($jspath, $etag) {
global $CFG;
require("$CFG->dirroot/lib/xsendfilelib.php");

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

header('Etag: '.$etag);
header('Content-Disposition: inline; filename="javascript.php"');
header('Last-Modified: '. gmdate('D, d M Y H:i:s', filemtime($jspath)) .' GMT');
header('Expires: '. gmdate('D, d M Y H:i:s', time() + $lifetime) .' GMT');
Expand Down

0 comments on commit 8475b97

Please sign in to comment.