Speed Up your sites with the script below.
Join multiple javascript files into one file, compress them with gzip and set expiration header.
replace:
with your js files
Join multiple javascript files into one file, compress them with gzip and set expiration header.
replace:
PHP Code:
include('./js/file1.js');
include('./js/file2.js');
include('./js/file3.js');
PHP Code:
<?php
$offset = 60 * 60 * 24 * 30; // Cache for a day
header("Content-type: text/javascript");
header ("Cache-Control: max-age=" . $offset . ", must-revalidate");
header ("Expires: " . gmdate ("D, d M Y H:i:s", time() + $offset) . " GMT");
function compress($buffer) {
/* remove comments and empty lines */
//$buffer = preg_replace('!/\*.*?\*/!s', '', $buffer);
$buffer = preg_replace('(// .+)', '', $buffer);
$buffer = str_replace(array("//\r\n", "//\n"), '', $buffer);
$buffer = preg_replace('/\n\s*\n/', "\n", $buffer);
return $buffer;
}
ob_start();
include('./js/file1.js');
include('./js/file2.js');
include('./js/file3.js');
$out = ob_get_contents();
ob_end_clean();
$out = compress($out);
if(strstr($_SERVER["HTTP_ACCEPT_ENCODING"],"gzip"))
{
header("Content-Encoding: gzip");
header("Vary: Accept-Encoding");
ob_start('ob_gzhandler');
}
echo $out;
?>