Skip to content

Commit

Permalink
Use server side less only if NOT development (otherwise use js)
Browse files Browse the repository at this point in the history
  • Loading branch information
ranguard committed Jan 21, 2014
1 parent 583f306 commit 5a0e850
Show file tree
Hide file tree
Showing 5 changed files with 47 additions and 41 deletions.
37 changes: 20 additions & 17 deletions app.psgi
Expand Up @@ -86,24 +86,27 @@ $app = Plack::Middleware::Assets->wrap(
);

use CHI;
my $cache = CHI->new(
driver => 'FastMmap',
root_dir => '/tmp/',
cache_size => '100k'
);

# Wrap up to serve lessc parsed files
$app = Plack::Middleware::MCLess->wrap($app,
cache => $cache,
cache_ttl => '30 minutes',
root => "root/static",
files => [
map {"root/static/less/$_.less"}
qw(
style
)
],
);
if( !$ENV{PLACK_ENV} || $ENV{PLACK_ENV} ne 'development' ) {

# Only need for live
my $cache = CHI->new( driver => 'File',
root_dir => '/tmp/less.cache'
);

# Wrap up to serve lessc parsed files
$app = Plack::Middleware::MCLess->wrap($app,
cache => $cache,
cache_ttl => "60 minutes",
root => "root/static",
files => [
map {"root/static/less/$_.less"}
qw(
style
)
],
);
}

Plack::Middleware::ReverseProxy->wrap(
sub {
Expand Down
5 changes: 5 additions & 0 deletions lib/MetaCPAN/Web/Controller/Root.pm
Expand Up @@ -77,6 +77,11 @@ Attempt to render a view, if needed.

sub end : ActionClass('RenderView') {
my ( $self, $c ) = @_;

# Pass through to the front end
if ( $ENV{PLACK_ENV} && $ENV{PLACK_ENV} eq 'development' ) {
$c->stash->{PLACK_ENV} = 'development';
}
$c->stash->{req} = $c->req;
$c->stash->{api} = $c->config->{api};
$c->stash->{api_secure} = $c->config->{api_secure} || $c->config->{api};
Expand Down
22 changes: 8 additions & 14 deletions lib/Plack/Middleware/MCLess.pm
Expand Up @@ -25,22 +25,15 @@ sub new {
my $class = shift;
my $self = $class->SUPER::new(@_);

if( $ENV{PLACK_ENV} && $ENV{PLACK_ENV} eq 'development' ) {
warn "Use less.min.js - better for development";
return $self;
}

my $less = `lessc -v`;
croak("Can't find lessc command") unless $less;

if( $ENV{PLACK_ENV} && $ENV{PLACK_ENV} eq 'development' ) {
# No caching, always build fresh

# Could have an extra ENV, say 'CSS_DEV' or something
# so default cache to 10 mins but 0 if CSS_DEV but that
# seems like more work for someone to help patch so for now
# take the processing hit - see once this is merged if
# this is a problem, even a few seconds would help
$self->cache_ttl('0');
$self->expires('0');
} else {
$self->cache_ttl('30 minutes') unless $self->cache_ttl;
}
$self->cache_ttl('30 minutes') unless $self->cache_ttl;

$self->_build_content;

Expand Down Expand Up @@ -93,7 +86,8 @@ sub _get_content {
my $self = shift;

my $content = $self->cache->get($self->key);
return $content if $content;
return $content if defined $content;

return $self->_build_content;
}

Expand Down
9 changes: 7 additions & 2 deletions root/wrapper.html
Expand Up @@ -37,9 +37,14 @@
<meta name="description" content="<% meta_description %>" />
<%- END %>
<link rel="icon" type="image/png" href="/static/icons/favicon.png" />
<link rel="stylesheet/less" type="text/css" href="/static/less/style.less">
<script src="<% req.env.item('psgix.assets').0 %>" type="text/javascript"></script>

<% IF PLACK_ENV == 'development' %>
<link rel="stylesheet/less" type="text/css" href="/static/less/style.less">
<script src="/static/js/less.min.js" type="text/javascript"></script>
<% ELSE %>
<link href="<% req.env.item('psgix.assets_less').0 %>" rel="stylesheet" type="text/css">
<% END %>
<script type="text/javascript">

var _gaq = _gaq || [];
Expand Down Expand Up @@ -93,7 +98,7 @@
<b class="caret"></b>
</a>
<ul class="dropdown-menu">

<li><a href="/account/identities">Identities</a></li>
<li><a href="/account/profile">Profile</a></li>
<li>
Expand Down
15 changes: 7 additions & 8 deletions t/plack/mcless.t
Expand Up @@ -6,19 +6,18 @@ use Test::More;
use Plack::Test;
use Plack::Builder;
use HTTP::Request::Common;
use Path::Class;
use Plack::Middleware::MCLess;

use CHI;

BEGIN {
$ENV{PLACK_ENV} = 'development';
}
use File::Temp qw/ tempdir /;

use Plack::Middleware::MCLess;
my $cache_dir = tempdir( CLEANUP => 1 );

use CHI;
my $cache = CHI->new(
driver => 'FastMmap',
root_dir => '/tmp/',
cache_size => '100k'
driver => 'File',
root_dir => $cache_dir,
);

my $app = builder {
Expand Down

0 comments on commit 5a0e850

Please sign in to comment.