views:

339

answers:

3

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?

+1  A: 

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>
S.Mark
Thanks for that! Great script you have there!But you know the tool minify (not the minifying in general) from google actually minifies AND sends the files with compression and then i would be doing it again with the server part (apache deflate on the fly). And with doing it twice i was wondering if i was creating overhead..? sorry if i wasn't clear...
Mark Nolan
You mean double gzipping? I guess it won't because browers would not know its need to do unzipping 2 times.
S.Mark
+1  A: 

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

Ignacio Vazquez-Abrams
oh i didn't know gzip uses zlib libraries.. i thought that was the difference between gzip and deflate. gzip using well gzip libraries and deflate using zlib libraries. but the question is will the file be double compressed - i'm sure it won't get much smaller - but the client would have to first decrompress the deflatet and then the minified files - the same for the server. so there must be overhead there..?
Mark Nolan
If you already know it won't get smaller, then why do it? :)
Bart van Heukelom
+1  A: 

Minify doesn't serve the files through Apache, so there's no double-encoding.

With the DEFLATE filter, Apache gzips the requested file on-the-fly each time. Minify gzips the file on the first request then sends the pre-gzipped cached version for later requests.

Being PHP-based it trades performance for flexibility and ease-of-maintenance, but if you throw a proxy cache in front of it it'll perform as well as S.Mark's configuration.

mrclay