Sneller css en javascript inladen revisted
augustus 26th, 2009
Om even terug te komen op het vorige artikel, het blijkt dat op webkit gebaseerde browsers, c.q. Chrome en Safari niet kunnen omgaan met gzip.
Daarom is het slim om de volgende toevoegingen te maken in de code
if(preg_match('/Chrome/i', $_SERVER['HTTP_USER_AGENT'], $matches) || preg_match('/Safari/i', $_SERVER['HTTP_USER_AGENT'], $matches)) {
$encoding = 'none';
$cache = false;
}
Overigens heb ik de code helemaal omgebouwd naar OOP waarvoor hieronder de listing:
public function __construct() {
parent::__construct();
//poo($this);
$this->_LoadPage = false;
$this->cache = true;
$this->cachedir = HOME_DIR . '../tmp/cache';
$this->cssdir = HOME_DIR . 'css';
$this->jsdir = HOME_DIR . 'javascript';
}
public function combinecss() {
if($this->_CurrentId[1]) {
$use_me = explode('?',$this->_CurrentId);
if($use_me[1]) {
$use_me2 = explode('=',$use_me[1]);
$elements = explode(',',$use_me2[1]);
}
}
$base = realpath($this->cssdir);
//print $base;exit();
//chrome en safari kunnen niet omgaan met gegzipte css
if(preg_match('/Chrome/i', $_SERVER['HTTP_USER_AGENT'], $matches) || preg_match('/Safari/i', $_SERVER['HTTP_USER_AGENT'], $matches)) {
$encoding = 'none';
$this->cache = false;
}
if(!strstr($_SERVER['HTTP_USER_AGENT'], 'Opera') &&
preg_match('/^Mozilla\/4\.0 \(compatible; MSIE ([0-9]\.[0-9])/i', $_SERVER['HTTP_USER_AGENT'], $matches)) {
$version = floatval($matches[1]);
if ($version < 6)
$encoding = 'none';
if ($version == 6 && !strstr($_SERVER['HTTP_USER_AGENT'], 'EV1'))
$encoding = 'none';
}
foreach($elements as $key=>$value) {
$file = realpath($cssdir . '/' . $value);
$lastmodified = max($lastmodified, filemtime($file));
}
// Determine last modification date of the files
$lastmodified = 0;
// Send Etag hash
$hash = $lastmodified . '-' . md5(implode(',',$elements));
header ("Etag: "" . $hash . """);
if (isset($_SERVER['HTTP_IF_NONE_MATCH'])
&& stripslashes($_SERVER['HTTP_IF_NONE_MATCH']) == '"' . $hash . '"') {
// Return visit and no modifications, so do not send anything
header ("HTTP/1.0 304 Not Modified");
header ('Content-Length: 0');
} else {
// First time visit or files were modified
if ($this->cache) {
// Determine supported compression method
$gzip = strstr($_SERVER['HTTP_ACCEPT_ENCODING'], 'gzip');
$deflate = strstr($_SERVER['HTTP_ACCEPT_ENCODING'], 'deflate');
// Determine used compression method
$encoding = $gzip ? 'gzip' : ($deflate ? 'deflate' : 'none');
// Check for buggy versions of Internet Explorer
if (!strstr($_SERVER['HTTP_USER_AGENT'], 'Opera') &&
preg_match('/^Mozilla\/4\.0 \(compatible; MSIE ([0-9]\.[0-9])/i', $_SERVER['HTTP_USER_AGENT'], $matches)) {
$version = floatval($matches[1]);
if ($version < 6)
$encoding = 'none';
if ($version == 6 && !strstr($_SERVER['HTTP_USER_AGENT'], 'EV1'))
$encoding = 'none';
}
//exclude chrome and safari
if(preg_match('/Chrome/i', $_SERVER['HTTP_USER_AGENT'], $matches) || preg_match('/Safari/i', $_SERVER['HTTP_USER_AGENT'], $matches)) {
$version = floatval($matches[1]);
$encoding = 'none';
}
// Try the cache first to see if the combined files were already generated
$cachefile = 'cache-' . $hash . '.css' . ($encoding != 'none' ? '.' . $encoding : '');
if (file_exists($this->cachedir . '/' . $cachefile)) {
if ($fp = fopen($this->cachedir . '/' . $cachefile, 'rb')) {
if ($encoding != 'none') {
header ("Content-Encoding: " . $encoding);
}
header ("Content-Type: text/css");
header ("Content-Length: " . filesize($this->cachedir . '/' . $cachefile));
fpassthru($fp);
fclose($fp);
exit;
}
}
}
// Get contents of the files
$contents = '';
reset($elements);
while (list(,$element) = each($elements)) {
$path = realpath($base . '/' . $element);
$contents .= file_get_contents($path);
}
//poo($elements);exit();
// Send Content-Type
header ("Content-Type: text/css");
Het volgende stukje lost een probleem op wat we hadden met de plek van de directories. Omdat deze nu namelijk vanuit controllername/methode worden aangeroepen. (c.q. http://www.domein.extensie/apptools/combinecss)
if (isset($encoding) && $encoding != 'none') {
//echo $contents;exit();
$contents = ereg_replace('../img/','../../../img/',$contents);
$contents = gzencode($contents, 9, $gzip ? FORCE_GZIP : FORCE_DEFLATE);
header ("Content-Encoding: " . $encoding);
header ('Content-Length: ' . strlen($contents));
echo $contents;
}
// Store cache
if ($this->cache) {
if ($fp = fopen($this->cachedir . '/' . $cachefile, 'wb')) {
fwrite($fp, $contents);
fclose($fp);
}
}
}
}
Categorieën: Ontwikkeling



