From ca2b464bb4f7cab9b83cd6e2508c6079e3f948cc Mon Sep 17 00:00:00 2001 From: Chris Smith Date: Sun, 18 Jan 2009 19:43:54 +0100 Subject: Add capability to send pre-compressed js & css files if the browser can accept them - save a gzipped version of js & css files at the same time as the uncompressed version is cache - basic content negotiation to send the compressed files - uses sendfile (for compressed or uncompressed versions) if config indicates its available darcs-hash:20090118184354-f07c6-66c5b465ab147d83de409708bab2c47d1dafcf8d.gz --- lib/exe/js.php | 28 ++++++++++++++++++++++++++-- 1 file changed, 26 insertions(+), 2 deletions(-) (limited to 'lib/exe/js.php') diff --git a/lib/exe/js.php b/lib/exe/js.php index 8101ecd50..1caa22b8d 100644 --- a/lib/exe/js.php +++ b/lib/exe/js.php @@ -64,7 +64,20 @@ function js_out(){ if(js_cacheok($cache,array_merge($files,$plugins))){ http_conditionalRequest(filemtime($cache)); if($conf['allowdebug']) header("X-CacheUsed: $cache"); - readfile($cache); + + // finally send output + if (http_accepts_gzip() && http_gzip_valid($cache)) { + header('Vary: Accept-Encoding'); + header('Content-Encoding: gzip'); + if (!http_sendfile($cache.'.gz')) readfile($cache.".gz"); +# } else if (http_accepts_deflate()) { +# header('Vary: Accept-Encoding'); +# header('Content-Encoding: deflate'); +# readfile($cache.".zip"); + } else { + if (!http_sendfile($cache)) readfile($cache); + } + return; } else { http_conditionalRequest(time()); @@ -147,9 +160,20 @@ function js_out(){ // save cache file io_saveFile($cache,$js); + copy($cache,"compress.zlib://$cache.gz"); // finally send output - print $js; + if (http_accepts_gzip()) { + header('Vary: Accept-Encoding'); + header('Content-Encoding: gzip'); + print gzencode($js,9,FORCE_GZIP); +# } else if (http_accepts_deflate()) { +# header('Vary: Accept-Encoding'); +# header('Content-Encoding: deflate'); +# print gzencode($js,9,FORCE_DEFLATE); + } else { + print $js; + } } /** -- cgit v1.2.3