Created
March 22, 2012 10:47
-
-
Save waghanza/2157650 to your computer and use it in GitHub Desktop.
Minify your CSS file / with GZIP
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package CSSMinify; | |
use nginx; | |
use CSS::Minifier qw(minify); | |
use IO::Compress::Gzip ; | |
sub handler { | |
my $r=shift; | |
my $cache_dir="/tmp"; # Cache directory where minified file will be | |
kept | |
my $cache_file=$r->uri; | |
$cache_file=~s!/!_!g; | |
$cache_file=join("/", $cache_dir, $cache_file); | |
my $uri=$r->uri; | |
my $filename=$r->filename; | |
return DECLINED unless -f $filename; | |
$r->header_out('Cache-Control', '30d, must-revalidate'); | |
$r->header_out('Content-Encoding'=>'gzip'); | |
$r->send_http_header; | |
return OK if $r->header_only; | |
if (! -f $cache_file) { | |
open(INFILE, $filename) or die "Error reading file: $!"; | |
my $output = minify(input => *INFILE); | |
close(INFILE); | |
my $gz = new IO::Compress::Gzip($cache_file, Minimal => 1, | |
AutoClose => 1, Append => 0) or return 0; | |
print $gz $output; | |
} | |
$r->sendfile($cache_file); | |
return OK; | |
} | |
1; | |
__END__ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment