Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Combining deflate and minify - am i creating overhead?

I minify my css and js files on the fly with google.codes minify. I have also set my .htaccess to use deflate on all my css and js files - the reason beeing some js files (like shadowbox and tinymce) reference to other js files in the code. So i'm compressing with apache deflate and also minify compresses some js and css files with gzip - am i creating overhead by doing this - first gzipping (minify) and then zlib (deflate) will run through again. Or will apache deflate ignore the already gzipped files having the attributes set by minify in the headers. Anyone have any experiences with this?

like image 864
Mark Nolan Avatar asked Jan 08 '10 01:01

Mark Nolan


2 Answers

Minifying + deflating/gzipping works great together.

I use mod rewrite to do that purpose, I have pre-built all the css/js files into 2 versions, original and .css.gz/.js.gz version.

Browser just send .js/.css request, server checks the existance of .js.gz/.css.gz and return gzipped content if certain conditions are matched.

So it does not matter for js/css file are loaded on the fly from js (for example your shadowbox or tinymce)

Basically, like this

RewriteEngine On
RewriteBase /

#Check for browser's Accept-Encoding,
RewriteCond "%{HTTP:Accept-Encoding}" "gzip.*deflate|deflate.*gzip"

#check file name is endswith css or js
RewriteCond %{REQUEST_FILENAME} "\.(css|js)$"

#check existance of .gz file name
RewriteCond %{REQUEST_FILENAME}.gz -s

#rewrite it to .js.gz or .css.gz
RewriteRule ^.*$ %{REQUEST_URI}.gz [L]

#update some response header
<FilesMatch "\.js\.gz$">
    AddEncoding gzip .gz
    ForceType "text/javascript"
</FilesMatch>

<FilesMatch "\.css\.gz$">
    AddEncoding gzip .gz
    ForceType "text/css"
</FilesMatch>
like image 136
YOU Avatar answered Sep 27 '22 21:09

YOU


gzip uses the zlib compression algorithm, and most byte sequences will not compress well the second time around.

like image 20
Ignacio Vazquez-Abrams Avatar answered Sep 27 '22 21:09

Ignacio Vazquez-Abrams