Since Meteora 0.4, we added support for compression. This is not enabled by default, because you'll need a server side script for doing the GZIP compression.
To enable compression, use this line before loading meteora:
Now Meteora will load all the scripts via the document.jscompressor, that script is expected to receive a src variable that contains the script subdirectories and names separed by commas, it is also expected to output the result of reading and joining all those scripts into a single one and compressing it, using cache when necessary.
Step by step procedure
Suppose that Meteora wants to load Meteora/Jsonrpc.js, Fx/Base.js and Fx/Css.js so it will query for: /path/to/compression/script.ext?src=Meteora.Jsonrpc,Fx.Base,Fx.CSS. The script, /path/to/compression/script.ext, will check that only letters, commans (,) and dots (.) are used in the src variable, then it will read from disk all the files it is asked to, after reading, it will write them to disk and send all them to the browser using GZIP compression without forgetting the appropiate headers.
PHP example
This is a PHP example of a compressor, it is optimized for speed, it does use cache too./**
* GZIP Compression for Meteora
* -----------------------
* Written by Jose Carlos Nieto <xiam@astrata.com.mx>
* (c) 2008 Astrata Software http://meteora.astrata.com.mx
*
*/
define('METEORA_LIBPATH', './meteora/lib/');
define('CACHE_DIR', '../temp/');
define('ENABLE_GZIP', true);
$src =& $_GET['src'];
error_reporting(0);
if (!empty($src)) {
$src = preg_replace('/[^a-zA-Z0-9\.,]/i', '', $src);
$etag = md5($src);
if ($_SERVER['HTTP_IF_NONE_MATCH'] == $etag) {
header('HTTP/1.0 304 Not Modified');
exit;
}
$files = explode(',', $src);
if ($files) {
header('Content-Type: text/javascript; charset=utf-8');
header('Cache-Meteora. public');
header('Expires: '.gmdate('D, d M Y H:i:s', time()+3600).' GMT');
header('Etag: '.$etag.'');
$cache_file = CACHE_DIR.'js_'.$etag;
if (file_exists($cache_file)) {
if (ENABLE_GZIP) {
header('Content-Encoding: gzip');
header('Vary: Accept-Encoding');
}
readfile($cache_file);
} else {
if (ENABLE_GZIP) {
ob_start('ob_gzhandler');
} else {
ob_start();
}
foreach($files as $file) {
$file = METEORA_LIBPATH.str_replace('.', '/', $file).'.js';
if (file_exists($file)) {
readfile($file);
}
}
$buff = ob_get_clean();
if (ENABLE_GZIP) {
$size = strlen($buff);
header('Content-Encoding: gzip');
header('Vary: Accept-Encoding');
$crc = crc32($buff);
$buff = "\x1f\x8b\x08\x00\x00\x00\x00\x00".substr(gzcompress($buff, 9), 0, -4);
$buff .= pack('V', $crc);
$buff .= pack('V', $size);
}
$fh = fopen($cache_file, 'w');
fwrite($fh, $buff);
fclose($fh);
echo $buff;
}
}
}
?>