Added javascript minification caching

This commit is contained in:
Timothy Warren 2011-09-07 17:08:57 -04:00
parent de8e0de77f
commit 444f7ec9d1
2 changed files with 21 additions and 6 deletions

View File

@ -7,4 +7,5 @@ A simple set of minifying scripts for CSS and Javascript
1. Figure out your file paths, and set them in css.php and js.php.
2. Add your css and javascript files to groups, in `config/css_groups.php` and `config/js_groups.php` respectively
3. Point your CSS links in your HTML to `css.php/g/[group_name]`, and likewise your javascript to `js.php/g/[group_name]`
4. Enjoy a faster loading website
4. Add a folder named "cache" to your js path
5. Enjoy a faster loading website

24
js.php
View File

@ -1,6 +1,7 @@
<?php
//Change as needed
$base_path = $_SERVER['DOCUMENT_ROOT'];
$cache_file = $base_path.'/cache/cache.js';
require('./config/jshrink.php');
@ -44,15 +45,18 @@ if(isset($groups[$_GET['g']]))
//Add this page too
$modified[] = filemtime($base_path."js.php");
$cache_modified = 0;
//Add the cache file
if(is_file($cache_file))
{
$cache_modified = filemtime($cache_file);
}
//Get the latest modified date
rsort($modified);
$last_modified = $modified[0];
if(!isset($_GET['debug']))
{
$js = JShrink::minify($js, array('flaggedComments' => false));
}
$requested_time=(isset($_SERVER['HTTP_IF_MODIFIED_SINCE']))
? strtotime($_SERVER['HTTP_IF_MODIFIED_SINCE'])
: time();
@ -63,6 +67,16 @@ if($last_modified === $requested_time)
exit();
}
if(!isset($_GET['debug']) && ($cache_modified < $last_modified))
{
$js = trim(JShrink::minify($js, array('flaggedComments' => false)));
file_put_contents($cache_file, $js);
}
else
{
$js = file_get_contents($cache_file);
}
header("Content-Type: application/x-javascript; charset=utf8");
header("Cache-control: public, max-age=691200, must-revalidate");
header("Last-Modified: ".gmdate('D, d M Y H:i:s', $last_modified)." GMT");