mirror of
https://github.com/mrclay/minify.git
synced 2025-08-13 09:34:54 +02:00
Resolved issue 37: replaced 'setExpires' with 'maxAge' option which allows both Expires headers and conditional GETs (if a page should be reloaded in IE, e.g.)
This commit is contained in:
@@ -93,10 +93,10 @@ class Minify {
|
|||||||
* 'contentTypeCharset' : appended to the Content-Type header sent. Set to a falsey
|
* 'contentTypeCharset' : appended to the Content-Type header sent. Set to a falsey
|
||||||
* value to remove. (default 'UTF-8')
|
* value to remove. (default 'UTF-8')
|
||||||
*
|
*
|
||||||
* 'setExpires' : set this to a timestamp to have Minify send an HTTP Expires
|
* 'maxAge' : set this to the number of seconds the client should use its cache
|
||||||
* header instead of checking for conditional GET (default null).
|
* before revalidating with the server. This sets Cache-Control: max-age and the
|
||||||
* E.g. ($_SERVER['REQUEST_TIME'] + 86400 * 365) for 1yr
|
* Expires header. Unlike the old 'setExpires' setting, this setting will NOT
|
||||||
* Note this has nothing to do with server-side caching.
|
* prevent conditional GETs. Note this has nothing to do with server-side caching.
|
||||||
*
|
*
|
||||||
* 'debug' : set to true to minify all sources with the 'Lines' controller, which
|
* 'debug' : set to true to minify all sources with the 'Lines' controller, which
|
||||||
* eases the debugging of combined files. This also prevents 304 responses.
|
* eases the debugging of combined files. This also prevents 304 responses.
|
||||||
@@ -148,6 +148,7 @@ class Minify {
|
|||||||
$options = $controller->analyzeSources($options);
|
$options = $controller->analyzeSources($options);
|
||||||
self::$_options = $controller->mixInDefaultOptions($options);
|
self::$_options = $controller->mixInDefaultOptions($options);
|
||||||
|
|
||||||
|
// check request validity
|
||||||
if (! $controller->sources) {
|
if (! $controller->sources) {
|
||||||
// invalid request!
|
// invalid request!
|
||||||
if (! self::$_options['quiet']) {
|
if (! self::$_options['quiet']) {
|
||||||
@@ -169,44 +170,36 @@ class Minify {
|
|||||||
|
|
||||||
if (self::$_options['debug']) {
|
if (self::$_options['debug']) {
|
||||||
self::_setupDebug($controller->sources);
|
self::_setupDebug($controller->sources);
|
||||||
self::$_options['setExpires'] = time();
|
self::$_options['maxAge'] = 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (null === self::$_options['setExpires']) {
|
// check client cache
|
||||||
// conditional GET
|
require_once 'HTTP/ConditionalGet.php';
|
||||||
require_once 'HTTP/ConditionalGet.php';
|
$cgOptions = array(
|
||||||
$cg = new HTTP_ConditionalGet(array(
|
'lastModifiedTime' => self::$_options['lastModifiedTime']
|
||||||
'lastModifiedTime' => self::$_options['lastModifiedTime']
|
,'isPublic' => self::$_options['isPublic']
|
||||||
,'isPublic' => self::$_options['isPublic']
|
);
|
||||||
));
|
if (null !== self::$_options['maxAge']) {
|
||||||
if ($cg->cacheIsValid) {
|
$cgOptions['maxAge'] = self::$_options['maxAge'];
|
||||||
// client's cache is valid
|
}
|
||||||
if (! self::$_options['quiet']) {
|
$cg = new HTTP_ConditionalGet($cgOptions);
|
||||||
$cg->sendHeaders();
|
if ($cg->cacheIsValid) {
|
||||||
return;
|
// client's cache is valid
|
||||||
} else {
|
if (! self::$_options['quiet']) {
|
||||||
return array(
|
$cg->sendHeaders();
|
||||||
'success' => true
|
return;
|
||||||
,'statusCode' => 304
|
|
||||||
,'content' => ''
|
|
||||||
,'headers' => array()
|
|
||||||
);
|
|
||||||
}
|
|
||||||
} else {
|
} else {
|
||||||
// client will need output
|
return array(
|
||||||
$headers = $cg->getHeaders();
|
'success' => true
|
||||||
unset($cg);
|
,'statusCode' => 304
|
||||||
|
,'content' => ''
|
||||||
|
,'headers' => $cg->getHeaders()
|
||||||
|
);
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
// don't need conditional GET
|
// client will need output
|
||||||
$privacy = self::$_options['isPublic']
|
$headers = $cg->getHeaders();
|
||||||
? 'public'
|
unset($cg);
|
||||||
: 'private';
|
|
||||||
$headers = array(
|
|
||||||
'Cache-Control' => $privacy . ', max-age='
|
|
||||||
. (self::$_options['setExpires'] - $_SERVER['REQUEST_TIME'])
|
|
||||||
,'Expires' => gmdate('D, d M Y H:i:s \G\M\T', self::$_options['setExpires'])
|
|
||||||
);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// determine encoding
|
// determine encoding
|
||||||
@@ -226,6 +219,7 @@ class Minify {
|
|||||||
self::$_options['encodeMethod'] = ''; // identity (no encoding)
|
self::$_options['encodeMethod'] = ''; // identity (no encoding)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// check server cache
|
||||||
if (null !== self::$_cache) {
|
if (null !== self::$_cache) {
|
||||||
// using cache
|
// using cache
|
||||||
// the goal is to use only the cache methods to sniff the length and
|
// the goal is to use only the cache methods to sniff the length and
|
||||||
|
@@ -45,7 +45,7 @@ abstract class Minify_Controller_Base {
|
|||||||
,'encodeLevel' => 9
|
,'encodeLevel' => 9
|
||||||
,'minifierOptions' => array() // no minifier options
|
,'minifierOptions' => array() // no minifier options
|
||||||
,'contentTypeCharset' => 'UTF-8'
|
,'contentTypeCharset' => 'UTF-8'
|
||||||
,'setExpires' => null // use conditional GET
|
,'maxAge' => null // no Expires/max-age
|
||||||
,'quiet' => false // serve() will send headers and output
|
,'quiet' => false // serve() will send headers and output
|
||||||
,'debug' => false
|
,'debug' => false
|
||||||
|
|
||||||
|
@@ -23,7 +23,12 @@ function test_Minify()
|
|||||||
'success' => true
|
'success' => true
|
||||||
,'statusCode' => 304
|
,'statusCode' => 304
|
||||||
,'content' => '',
|
,'content' => '',
|
||||||
'headers' => array()
|
'headers' => array(
|
||||||
|
'Last-Modified' => gmdate('D, d M Y H:i:s \G\M\T', $lastModified),
|
||||||
|
'ETag' => "\"{$lastModified}pub\"",
|
||||||
|
'Cache-Control' => 'max-age=0, public, must-revalidate',
|
||||||
|
'_responseCode' => 'HTTP/1.0 304 Not Modified',
|
||||||
|
)
|
||||||
);
|
);
|
||||||
$output = Minify::serve('Files', array(
|
$output = Minify::serve('Files', array(
|
||||||
'files' => $thisDir . '/_test_files/css/styles.css' // controller casts to array
|
'files' => $thisDir . '/_test_files/css/styles.css' // controller casts to array
|
||||||
@@ -43,20 +48,24 @@ function test_Minify()
|
|||||||
//! class_exists('Cache_Lite_File', false)
|
//! class_exists('Cache_Lite_File', false)
|
||||||
! class_exists('HTTP_Encoder', false)
|
! class_exists('HTTP_Encoder', false)
|
||||||
&& ! class_exists('Minify_CSS', false)
|
&& ! class_exists('Minify_CSS', false)
|
||||||
,'Encoder.php, CSS.php not loaded'
|
&& ! class_exists('Minify_Cache', false)
|
||||||
|
,'Encoder.php, CSS.php, Cache.php not loaded'
|
||||||
);
|
);
|
||||||
|
|
||||||
// Test minifying JS and serving with Expires header
|
// Test minifying JS and serving with Expires header
|
||||||
|
|
||||||
$content = preg_replace('/\\r\\n?/', "\n", file_get_contents($minifyTestPath . '/minified.js'));
|
$content = preg_replace('/\\r\\n?/', "\n", file_get_contents($minifyTestPath . '/minified.js'));
|
||||||
|
$lastModified = filemtime($minifyTestPath . '/minified.js');
|
||||||
$expected = array(
|
$expected = array(
|
||||||
'success' => true
|
'success' => true
|
||||||
,'statusCode' => 200
|
,'statusCode' => 200
|
||||||
// Minify_Javascript always converts to \n line endings
|
// Minify_Javascript always converts to \n line endings
|
||||||
,'content' => $content
|
,'content' => $content
|
||||||
,'headers' => array (
|
,'headers' => array (
|
||||||
'Cache-Control' => 'public, max-age=86400',
|
'Expires' => gmdate('D, d M Y H:i:s \G\M\T', $tomorrow),
|
||||||
'Expires' => gmdate('D, d M Y H:i:s \G\M\T', $tomorrow),
|
'Last-Modified' => gmdate('D, d M Y H:i:s \G\M\T', $lastModified),
|
||||||
|
'ETag' => "\"{$lastModified}pub\"",
|
||||||
|
'Cache-Control' => 'max-age=86400, public, must-revalidate',
|
||||||
'Content-Length' => strlen($content),
|
'Content-Length' => strlen($content),
|
||||||
'Content-Type' => 'application/x-javascript; charset=UTF-8',
|
'Content-Type' => 'application/x-javascript; charset=UTF-8',
|
||||||
)
|
)
|
||||||
@@ -67,7 +76,7 @@ function test_Minify()
|
|||||||
,$minifyTestPath . '/QueryString.js'
|
,$minifyTestPath . '/QueryString.js'
|
||||||
)
|
)
|
||||||
,'quiet' => true
|
,'quiet' => true
|
||||||
,'setExpires' => $tomorrow
|
,'maxAge' => 86400
|
||||||
,'encodeOutput' => false
|
,'encodeOutput' => false
|
||||||
));
|
));
|
||||||
$passed = assertTrue($expected === $output, 'Minify : JS and Expires');
|
$passed = assertTrue($expected === $output, 'Minify : JS and Expires');
|
||||||
|
Reference in New Issue
Block a user