compress html

466 views
Skip to first unread message

Tony Xiao

unread,
Jan 18, 2011, 4:10:35 AM1/18/11
to play-framework
Is there a way to either tidy up or compress the html page that's
generated by play template engine? I usually end up with tremendous
amount of white space and it costs quite a bit of bandwidth and more
importantly load speed.

So basically something like greenscript for html?

Steve Chaloner

unread,
Jan 18, 2011, 4:59:56 AM1/18/11
to play-framework
You could try adding a @Finally method to your controllers which acts
on the contents of response.out, but this is just off the top of my
head - no idea if it works or not.

Justin Holmes

unread,
Jan 18, 2011, 6:33:52 AM1/18/11
to play-framework
http://code.google.com/p/htmlcompressor/

String html = getHtml(); //your external method to get html from
memory, file, url etc.
HtmlCompressor compressor = new HtmlCompressor();
String compressedHtml = compressor.compress(html);

You could write a plugin which scans your views directory and then
writes out the compressed version

Justin Holmes

unread,
Jan 18, 2011, 6:36:05 AM1/18/11
to play-framework
or just run it using the command line

java -jar htmlcompressor-0.9.6.jar --compress-js /path/original/
test.html > /path/compressed/test-compressed.html

Guillaume Bort

unread,
Jan 18, 2011, 6:53:05 AM1/18/11
to play-fr...@googlegroups.com
If bandwidth is your main concern you should activate Gzip compression
on your proxy server.

http://httpd.apache.org/docs/2.0/mod/mod_deflate.html

> --
> You received this message because you are subscribed to the Google Groups "play-framework" group.
> To post to this group, send email to play-fr...@googlegroups.com.
> To unsubscribe from this group, send email to play-framewor...@googlegroups.com.
> For more options, visit this group at http://groups.google.com/group/play-framework?hl=en.
>
>

--
Guillaume Bort, http://guillaume.bort.fr

For anything work-related, use g...@zenexity.fr; for everything else,
write guillau...@gmail.com

Tony Xiao

unread,
Jan 18, 2011, 7:02:22 PM1/18/11
to play-fr...@googlegroups.com
Ah, good to know. I'm running play by itself at the moment but will certainly use the mod when Apache comes into play (no pun intended). 

Tony Xiao

unread,
Jan 18, 2011, 7:04:19 PM1/18/11
to play-fr...@googlegroups.com
Cool stuff, do you think it would be more efficient to compress the template itself rather than to compress the output every time a request comes in? 

How easy would it be to write a plugin that does nothing other than compressing templates? Would it be worth the effort?

Kim Kha Nguyễn

unread,
Feb 21, 2012, 6:15:00 AM2/21/12
to play-fr...@googlegroups.com
It works for me. Add this to your controller.

@Finally
static void compress() throws IOException {
    if (Play.mode.isProd()) {
        String html = response.out.toString();

        HtmlCompressor compressor = new HtmlCompressor();
        compressor.setRemoveIntertagSpaces(true);
        compressor.setRemoveSurroundingSpaces("br,p,span,a");
        compressor.setCompressCss(true);

        String compressedHtml = compressor.compress(html);
        response.out = new ByteArrayOutputStream(4000);
        response.out.write(compressedHtml.getBytes());
    }
}


You must add HTMLCompressor and YUI Compressor to your build path

Dmitry Petrashko

unread,
Jun 13, 2012, 4:56:32 PM6/13/12
to play-fr...@googlegroups.com
Hi!
Do you know how to add this to play 2.0?

Thanks in advance!

Marcel Klemenz

unread,
Jun 14, 2012, 7:29:31 AM6/14/12
to play-fr...@googlegroups.com
please have a look at minifymod https://groups.google.com/forum/#!topic/play-framework/x3SNGni4ZkA
it supports minify and gzip responses in play one.

greets,
maklemenz

Federico Tolomei

unread,
Jun 14, 2012, 10:48:07 AM6/14/12
to play-fr...@googlegroups.com
Is there something similar available for play two ? As far I can see
the interceptors has not access to the response output...

--
skype: effe.to
Reply all
Reply to author
Forward
0 new messages