[Catalyst] post processing filter in Catalyst apps

Rajesh Kumar Mallah mallah at redgrape.tech
Mon Feb 20 08:34:50 GMT 2017


Dear Dimitar,


the verXXX change everytime there is git commit.
I was able to solve my problem completely for development environment.

For production i would serve static from from apache or lighttpd
with a rewrite rule that would strip off the version numbers.
Embedding version number in urls of css and js was just for
defeating browser cache.

I am posting the Root.pm below for reference:

sub end : Private {
        my ($self, $c) = @_;

       # in case of static files do not render view.

        return if ($c->stash->{file_output}) ;


        $c->forward('render') unless $c->res->output ;
        $c->fillform($c->stash->{fillindata}) ;

        my $body = \$c->res->body;

        my $imgversion   = $c->config->{imgversion}  ;
        my $cssversion   = $c->config->{cssversion}  ;
        my $jsversion    = $c->config->{jsversion}   ;
        my $bowerversion = $c->config->{bowerversion};

        # below code injects versions into urls.

        $$body =~ s#/static/images/#/static/ver-$imgversion/images/#g;
        $$body =~ s#/static/css/#/static/ver-$cssversion/css/#g;
        $$body =~ s#/static/js/#/static/ver-$jsversion/js/#g;
        $$body =~
s#/static/bower_components/#/static/ver-$bowerversion/bower_components/#g;

        $c->res->body($$body);
}


# below uses deprecated Regex action ( shall be improved in future)

sub versioned_static_files : Regex('static/ver-.*?/(.*$)') {
  my ( $self, $c ) = @_;
  my $path =  $c->req->captures->[0];
  $c->serve_static_file($c->config->{appdir} . '/root/static/' .  $path);
  $c->stash->{file_output} = 1;

}


# somewhere in my App.pm


# code for getting the last commit hash
# for the application repositories
# code assumes that repository is being
# maintained in git.

my $appdir = __PACKAGE__->path_to( '.' ) ;
__PACKAGE__->config->{appdir}   = $appdir  ;

__PACKAGE__->log->debug("appdir is : $appdir");

my $oldcwd = cwd();
chdir $appdir ;

my $appversion    = `/usr/bin/git log --format="%h" -n 1`                 
               ;
my $feversion     = `/usr/bin/git log --format="%h" -n 1 -- root`         
               ;
my $imgversion    = `/usr/bin/git log --format="%h" -n 1 --
root/static/images`           ;
my $cssversion    = `/usr/bin/git log --format="%h" -n 1 --
root/static/css`              ;
my $jsversion     = `/usr/bin/git log --format="%h" -n 1 --
root/static/js`               ;
my $bowerversion  = `/usr/bin/git log --format="%h" -n 1 --
root/static/bower_components` ;

chomp
($appversion,$feversion,$imgversion,$cssversion,$jsversion,$bowerversion);

__PACKAGE__->config->{appversion}   = $appversion  ;
__PACKAGE__->config->{imgversion}   = $imgversion  ;
__PACKAGE__->config->{cssversion}   = $cssversion  ;
__PACKAGE__->config->{jsversion}    = $jsversion   ;
__PACKAGE__->config->{bowerversion} = $bowerversion;

__PACKAGE__->config->{backend_version}    = $appversion  ;
__PACKAGE__->config->{frontend_version}   = $feversion   ;

__PACKAGE__->log->debug("versions: appversion:$appversion
feversion:$feversion imgversion:$imgversion cssversion:$cssversion
jsversion:$jsversion bowerversion:$bowerversion");


chdir $oldcwd;




> Why don't you create a symlink verXXX to images at your server?
>
> Cheers
>
>> Hi , I was able to do so in Controller/Root.pm sub end : Private { my
>> ($self, $c) = @_; $c->forward('render') unless $c->res->output
>> ; $c->fillform($c->stash->{fillindata}) ; >>>> my
>> $body = $c->response->{body}; >>>> $body =~
>> s#/static/#/static/ver8291/#g ; >>>>
>> $c->response->output($body); } Thanks. Regds
>







More information about the Catalyst mailing list