Permalink
Browse files

Use Plack::MIME to properly detect content types, if everything is of…

… the same content types set that correctly.
  • Loading branch information...
jshirley committed Oct 10, 2011
1 parent 9b88ace commit 2b3e65caffc33480ba8e39c987e8c92f40d9b692
Showing with 22 additions and 5 deletions.
  1. +0 −1 dist.ini
  2. +15 −4 lib/Plack/Middleware/ComboLoader.pm
  3. +7 −0 t/simple.t
View
@@ -8,5 +8,4 @@ copyright_holder= Infinity Interactive, Inc
[Prereqs]
Plack = 0
HTTP::Date = 0
-MIME::Types = 0
HTTP::Throwable = 0
@@ -8,7 +8,7 @@ use Carp 'carp';
use Plack::Request;
use Path::Class;
-use MIME::Types;
+use Plack::MIME;
use Try::Tiny;
use HTTP::Throwable::Factory qw(http_throw http_exception);
@@ -33,14 +33,16 @@ __PACKAGE__->mk_accessors(qw( roots patterns save expires));
path => 'static/css',
processor => sub {
# $_ isa Path::Class::File object
- JavasSript::Minify::minify( input => $_->slurp );
+ # It is much, much better to minify as a build process
+ # and not on demand.
+ CSS::Minifier::minify( input => $_->slurp );
# This method returns a *string*
}
}
},
# Optional parameter to save generated files to this path:
# If the file is there and it's not too old, it gets served.
- # If it is too old (the expires below), it will be regenerated
+ # If it is too old (the expires below), it will be regenerated.
save => 'static/combined',
expires => 86400, # Keep files around for a day.
$app;
@@ -122,8 +124,10 @@ sub call {
}
}
- my $buffer = '';
+ my $buffer = '';
my $last_modified = 0;
+ my %seen_types = ();
+
foreach my $resource ( @resources ) {
my $f = $dir->file($resource);
my $stat = $f->stat;
@@ -133,6 +137,7 @@ sub call {
});
}
+ $seen_types{ Plack::MIME->mime_type($f->basename) || 'text/plain' } = 1;
# Set the last modified to the most recent file.
$last_modified = $stat->mtime if $stat->mtime > $last_modified;
@@ -157,6 +162,12 @@ sub call {
print $fh $buffer;
$fh->close;
}
+
+ # We only encountered one content-type, rejoice, for we can set one
+ # sensibly!
+ if ( scalar keys %seen_types == 1 ) {
+ ( $content_type ) = keys %seen_types;
+ }
$res->content_type($content_type);
$res->header('Last-Modified' => HTTP::Date::time2str( $last_modified ) );
$res->content($buffer);
View
@@ -24,7 +24,13 @@ test_psgi $loader => sub {
subtest "simple concat" => sub {
my $res = $server->(GET '/t1?foo.js&bar.js');
ok($res->is_success, 'valid request');
+ is($res->content_type, 'application/javascript', 'right content type');
is($res->content, qq{var foo = 1;\nvar bar = 2;\n}, 'right content');
+
+ $res = $server->(GET '/t1?foo.js&bar.js&plain.txt');
+ ok($res->is_success, 'valid request');
+ is($res->content_type, 'plain/text', 'right mixed content type');
+ is($res->content, qq{var foo = 1;\nvar bar = 2;\nhello\n}, 'right content');
};
subtest "missing files" => sub {
@@ -34,6 +40,7 @@ test_psgi $loader => sub {
is($res->code, 500, 'bad request');
is($res->content, q{400 Bad Request Invalid resource requested: `missing.js` is not available.}, 'right error message');
};
+
};
done_testing;

0 comments on commit 2b3e65c

Please sign in to comment.