You are viewing a plain text version of this content. The canonical link for it is here.
Posted to modperl@perl.apache.org by Graham TerMarsch <mo...@howlingfrog.com> on 2007/09/19 19:24:40 UTC

RFC: Apache2::Filter::Minifier::JavaScript

I've been working on some projects needing JS minification recently and wanted 
to ping others and find out if anyone else would find it useful to have a 
mod_perl2 filter that auto-minified your JS (using JavaScript::Minifier)...

Am thinking along the lines of something that you'd set up in your Apache 
config like:

	<FilesMatch "\.js$">
		PerlOutputFilterHandler Apache2::Filter::Minifier::JavaScript
		PerlSetVar MINIFIER_CACHE_ROOT /path/to/your/cache/dir
	</FilesMatch>

Results would be cached to disk, so that we didn't have to re-minify the JS 
files on each and every request.  Obviously, if the original file changes, 
the cached copy would be considered invalid.

Seeing the above makes me wonder, though... is there a way to set up a Perl 
output filter for -just- specific MIME-Types?  Similarly to 
AddOutputFilterByType, but for mod_perl handlers?

Thought I'd ask and see if others would find something like this useful, or if 
you've got suggestions on other ways to do this.  I know that I could just 
minify all the JS files on disk, but it'd be a whole lot simpler to just set 
it up as a filter and not have to worry about it.  Also means that I could 
turn the filter off during testing and be able to work with the un-minified 
JS.

Also thinking that an Apache2::Filter::Minifier::CSS module would be possible 
using CSS::Minifier; basically same thing but for CSS instead.

-- 
Graham TerMarsch

Re: RFC: Apache2::Filter::Minifier::JavaScript

Posted by Geoffrey Young <ge...@modperlcookbook.org>.

Graham TerMarsch wrote:
> On Thursday 20 September 2007 6:45 am, Geoffrey Young wrote:
>> Graham TerMarsch wrote:
>>> I've been working on some projects needing JS minification recently and
>>> wanted to ping others and find out if anyone else would find it useful to
>>> have a mod_perl2 filter that auto-minified your JS (using
>>> JavaScript::Minifier)...
>> see also
>>
>>   http://search.cpan.org/src/GEOFF/Apache-Clean-2.00_7/
>>   http://www.perl.com/pub/a/2003/04/17/filters.html
> 
> I'd seen HTML::Clean before, and had considered making a separate minifier 
> module along those lines.  I actually hadn't seen that version of it, though; 
> I missed the dev releases.
> 
> Thanks for the pointer though Geoff, I'll have a look at the latest 
> Apache::Clean release and see what useful tidbits of info are in there that 
> I've missed.

the parts you'll be interested in are the parts you were asking about,
like only running for a specific content-type.  it also has a basic
framework for using the streaming filter API and dealing with filter
context, as well as handling http compliance (removing the
content-length header, for example).  in all, you ought to be able to
use the base of it and swap the HTML::Clean innards for the purifier of
your choice.

good luck.

--Geoff

Re: RFC: Apache2::Filter::Minifier::JavaScript

Posted by Graham TerMarsch <mo...@howlingfrog.com>.
On Thursday 20 September 2007 6:45 am, Geoffrey Young wrote:
> Graham TerMarsch wrote:
> > I've been working on some projects needing JS minification recently and
> > wanted to ping others and find out if anyone else would find it useful to
> > have a mod_perl2 filter that auto-minified your JS (using
> > JavaScript::Minifier)...
>
> see also
>
>   http://search.cpan.org/src/GEOFF/Apache-Clean-2.00_7/
>   http://www.perl.com/pub/a/2003/04/17/filters.html

I'd seen HTML::Clean before, and had considered making a separate minifier 
module along those lines.  I actually hadn't seen that version of it, though; 
I missed the dev releases.

Thanks for the pointer though Geoff, I'll have a look at the latest 
Apache::Clean release and see what useful tidbits of info are in there that 
I've missed.

-- 
Graham TerMarsch

Re: RFC: Apache2::Filter::Minifier::JavaScript

Posted by Geoffrey Young <ge...@modperlcookbook.org>.

Graham TerMarsch wrote:
> I've been working on some projects needing JS minification recently and wanted 
> to ping others and find out if anyone else would find it useful to have a 
> mod_perl2 filter that auto-minified your JS (using JavaScript::Minifier)...

see also

  http://search.cpan.org/src/GEOFF/Apache-Clean-2.00_7/

  http://www.perl.com/pub/a/2003/04/17/filters.html

HTH

--Geoff

Re: RFC: Apache2::Filter::Minifier::JavaScript

Posted by Graham TerMarsch <mo...@howlingfrog.com>.
On Thursday 20 September 2007 12:10 am, Philippe M. Chiasson wrote:
> Graham TerMarsch wrote:
> > I've been working on some projects needing JS minification recently and
> > wanted to ping others and find out if anyone else would find it useful to
> > have a mod_perl2 filter that auto-minified your JS (using
> > JavaScript::Minifier)...
> >
> > Am thinking along the lines of something that you'd set up in your Apache
> > config like:
> >
> > 	<FilesMatch "\.js$">
> > 		PerlOutputFilterHandler Apache2::Filter::Minifier::JavaScript
> > 		PerlSetVar MINIFIER_CACHE_ROOT /path/to/your/cache/dir
> > 	</FilesMatch>
> >
> > Results would be cached to disk, so that we didn't have to re-minify the
> > JS files on each and every request.  Obviously, if the original file
> > changes, the cached copy would be considered invalid.
>
> Sounds like an interesting module, and I certainly would use it to serve
> my JavaScript. One note however, why implement your own caching mechanisms
> when you could very simply just make use of the existing mod_cache
> framework ?

Oh right... mod_cache... forgot about that...

Anyone care to pipe in with a quick snippet of config that I could include in 
the docs? (its late/early and I'm not thinking clearly enough yet myself)

-- 
Graham TerMarsch

Re: RFC: Apache2::Filter::Minifier::JavaScript

Posted by "Philippe M. Chiasson" <go...@ectoplasm.org>.
Graham TerMarsch wrote:
> I've been working on some projects needing JS minification recently and wanted 
> to ping others and find out if anyone else would find it useful to have a 
> mod_perl2 filter that auto-minified your JS (using JavaScript::Minifier)...
> 
> Am thinking along the lines of something that you'd set up in your Apache 
> config like:
> 
> 	<FilesMatch "\.js$">
> 		PerlOutputFilterHandler Apache2::Filter::Minifier::JavaScript
> 		PerlSetVar MINIFIER_CACHE_ROOT /path/to/your/cache/dir
> 	</FilesMatch>
> 
> Results would be cached to disk, so that we didn't have to re-minify the JS 
> files on each and every request.  Obviously, if the original file changes, 
> the cached copy would be considered invalid.

Sounds like an interesting module, and I certainly would use it to serve
my JavaScript. One note however, why implement your own caching mechanisms
when you could very simply just make use of the existing mod_cache framework ?

Apart from that, looking forward to seeing it on CPAN.

------------------------------------------------------------------------
Philippe M. Chiasson     GPG: F9BFE0C2480E7680 1AE53631CB32A107 88C3A5A5
http://gozer.ectoplasm.org/       m/gozer\@(apache|cpan|ectoplasm)\.org/


Re: RFC: Apache2::Filter::Minifier::JavaScript

Posted by Boysenberry Payne <bo...@humaniteque.com>.
If there is anything I can do to help let me know.  I'd really like  
to have a small footprint
on my JS output, right now its still pretty big.

Boysenberry Payne
Habitat Life, Inc.
http://www.habitatlife.com/

On Sep 19, 2007, at 12:49 PM, Graham TerMarsch wrote:

> On Wednesday 19 September 2007 10:40 am, Boysenberry Payne wrote:
>> I could use something like this.  Right now I use Toolkit Template to
>> build from.
>> It already caches the JS file loaded from disk.  I would want to then
>> put it through
>> the minifier process and cache then serve.  Would you module allow
>> for this?
>
> That'd be the idea... it'd just be a generic "output filter" that  
> you'd have
> to hook up yourself into your existing JS pipeline.  In my initial  
> example I
> used <FilesMatch> to do it, but you could hook it up anywhere that  
> you can
> set up a PerlOutpuFilterHandler.  It'd just take the JS that it was  
> provided,
> minify it, then cache the minified version.  On subsequent  
> requests, it'd
> (somehow) figure out that the cached copy was valid and then either  
> use the
> cached copy or re-minify+re-cache as necessary.
>
> If we're dealing with -dynamically- generated JS, though, then I'll  
> need to
> think a bit more about how I deal with "is our cached copy valid/ 
> stale?", as
> that'd be done differently than if just serving static JS files.   
> I'll have a
> look at that, though, as that's a good use case.  I'd initially  
> thought of
> using the "r->finfo->inode()" as a cache key, but I don't think  
> that's there
> if I'm being handed dynamically generated JS.
>
> -- 
> Graham TerMarsch
>


Re: RFC: Apache2::Filter::Minifier::JavaScript

Posted by Graham TerMarsch <mo...@howlingfrog.com>.
On Wednesday 19 September 2007 10:40 am, Boysenberry Payne wrote:
> I could use something like this.  Right now I use Toolkit Template to
> build from.
> It already caches the JS file loaded from disk.  I would want to then
> put it through
> the minifier process and cache then serve.  Would you module allow
> for this?

That'd be the idea... it'd just be a generic "output filter" that you'd have 
to hook up yourself into your existing JS pipeline.  In my initial example I 
used <FilesMatch> to do it, but you could hook it up anywhere that you can 
set up a PerlOutpuFilterHandler.  It'd just take the JS that it was provided, 
minify it, then cache the minified version.  On subsequent requests, it'd 
(somehow) figure out that the cached copy was valid and then either use the 
cached copy or re-minify+re-cache as necessary.

If we're dealing with -dynamically- generated JS, though, then I'll need to 
think a bit more about how I deal with "is our cached copy valid/stale?", as 
that'd be done differently than if just serving static JS files.  I'll have a 
look at that, though, as that's a good use case.  I'd initially thought of 
using the "r->finfo->inode()" as a cache key, but I don't think that's there 
if I'm being handed dynamically generated JS.

-- 
Graham TerMarsch

Re: RFC: Apache2::Filter::Minifier::JavaScript

Posted by Boysenberry Payne <bo...@humaniteque.com>.
I could use something like this.  Right now I use Toolkit Template to  
build from.
It already caches the JS file loaded from disk.  I would want to then  
put it through
the minifier process and cache then serve.  Would you module allow  
for this?

-bop

On Sep 19, 2007, at 12:24 PM, Graham TerMarsch wrote:

> I've been working on some projects needing JS minification recently  
> and wanted
> to ping others and find out if anyone else would find it useful to  
> have a
> mod_perl2 filter that auto-minified your JS (using  
> JavaScript::Minifier)...
>
> Am thinking along the lines of something that you'd set up in your  
> Apache
> config like:
>
> 	<FilesMatch "\.js$">
> 		PerlOutputFilterHandler Apache2::Filter::Minifier::JavaScript
> 		PerlSetVar MINIFIER_CACHE_ROOT /path/to/your/cache/dir
> 	</FilesMatch>
>
> Results would be cached to disk, so that we didn't have to re- 
> minify the JS
> files on each and every request.  Obviously, if the original file  
> changes,
> the cached copy would be considered invalid.
>
> Seeing the above makes me wonder, though... is there a way to set  
> up a Perl
> output filter for -just- specific MIME-Types?  Similarly to
> AddOutputFilterByType, but for mod_perl handlers?
>
> Thought I'd ask and see if others would find something like this  
> useful, or if
> you've got suggestions on other ways to do this.  I know that I  
> could just
> minify all the JS files on disk, but it'd be a whole lot simpler to  
> just set
> it up as a filter and not have to worry about it.  Also means that  
> I could
> turn the filter off during testing and be able to work with the un- 
> minified
> JS.
>
> Also thinking that an Apache2::Filter::Minifier::CSS module would  
> be possible
> using CSS::Minifier; basically same thing but for CSS instead.
>
> -- 
> Graham TerMarsch


Re: RFC: Apache2::Filter::Minifier::JavaScript

Posted by Clinton Gormley <cl...@traveljury.com>.
On Wed, 2007-09-19 at 10:24 -0700, Graham TerMarsch wrote:
> I've been working on some projects needing JS minification recently and wanted 
> to ping others and find out if anyone else would find it useful to have a 
> mod_perl2 filter that auto-minified your JS (using JavaScript::Minifier)...
> 

I was about to reply: why minify, doesn't mod_deflate have such a
significant effect that any further processing is a waste of time?

But I thought I'd test it before commenting, and I was pleasantly
surprised:

I tried downloading the dragdrop file from YUI
(http://developer.yahoo.com/yui/), and these were my results:

 - no-mini, no-deflate   : 100kB
 - mini   , no-delate    : 23kB
 - no-mini, deflate      : 22kB
 - mini   , deflate      : 7kB

So yes, I think this module would be very useful indeed, especially (and
I haven't looked at the details) if you are able to join a number of JS
source files and return them as a single request.

Clint


Re: RFC: Apache2::Filter::Minifier::JavaScript

Posted by "Philippe M. Chiasson" <go...@ectoplasm.org>.
Graham TerMarsch wrote:
> I've been working on some projects needing JS minification recently and wanted 
> to ping others and find out if anyone else would find it useful to have a 
> mod_perl2 filter that auto-minified your JS (using JavaScript::Minifier)...
> [...]
> 
> Seeing the above makes me wonder, though... is there a way to set up a Perl 
> output filter for -just- specific MIME-Types?  Similarly to 
> AddOutputFilterByType, but for mod_perl handlers?

Not at this time, there is no PerlOutputFilterHandlerByType directive.
Most likely because you can implement it yourself in just a few lines
of Perl in a custom handler that just looks at the mime type and sets
up the filter you want.

No technical reason there couldn't be such a directive, but it would be
a might long and ugly name ;-)

------------------------------------------------------------------------
Philippe M. Chiasson     GPG: F9BFE0C2480E7680 1AE53631CB32A107 88C3A5A5
http://gozer.ectoplasm.org/       m/gozer\@(apache|cpan|ectoplasm)\.org/