Welcome! Log In Create A New Profile

Advanced

Minify and Gzip

Cédric Jeanneret
August 09, 2011 10:04AM
Hello,

I just wanted to share a perl script I made for nginx. Its aim is to:
- minify CSS (and by a simple modification JS)
- deflate it
- keep it as a local file

It's based on the nice http://wiki.nginx.org/NginxEmbeddedPerlMinifyJS
with the use of IO::Compress::Gzip.

Any comment/improvement is welcome.

Cheers,

C.


== Script ==

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__

_______________________________________________
nginx mailing list
nginx@nginx.org
http://mailman.nginx.org/mailman/listinfo/nginx
Subject Author Posted

Minify and Gzip

Cédric Jeanneret August 09, 2011 10:04AM



Sorry, only registered users may post in this forum.

Click here to login

Online Users

Guests: 262
Record Number of Users: 8 on April 13, 2023
Record Number of Guests: 421 on December 02, 2018
Powered by nginx      Powered by FreeBSD      PHP Powered      Powered by MariaDB      ipv6 ready