how to gzip-compress large Ajax responses (HTML only) in Coldfusion?

Posted by frequent on Stack Overflow See other posts from Stack Overflow or by frequent
Published on 2012-09-01T09:02:37Z Indexed on 2012/09/01 9:38 UTC
Read the original article Hit count: 265

Filed under:
|
|
|
|

I'm running Coldfusion8 and jquery/jquery-mobile on the front-end.

I'm playing around with an Ajax powered search engine trying to find the best tradeoff between data-volume and client-side processing time.

Currently my AJAX search returns 40k of (JQM-enhanced markup), which avoids any client-side enhancement. This way I'm getting by without the page stalling for about 2-3 seconds, while JQM enhances all elements in the search results.

What I'm curious is whether I can gzip Ajax responses sent from Coldfusion. If I check the header of my search right now, I'm having this:

    RESPONSE-header
    Connection  Keep-Alive
    Content-Type    text/html; charset=UTF-8
    Date    Sat, 01 Sep 2012 08:47:07 GMT
    Keep-Alive  timeout=5, max=95
    Server  Apache/2.2.21 (Win32) mod_ssl/2.2.21 ...
    Transfer-Encoding   chunked

    REQUEST-header
    Accept  */*
    Accept-Encoding gzip, deflate
    Accept-Language de-de,de;q=0.8,en-us;q=0.5,en;q=0.3
    Connection  keep-alive
    Cookie  CFID=   ; CFTOKEN=   ; resolution=1143
    Host    www.host.com
    Referer http://www.host.com/dev/users/index.cfm

So, my request would accept gzip, deflate, but I'm getting back chunked.

I'm generating the AJAX response in a cfsavecontent (called compressedHTML) and run this to eliminate whitespace

<cfrscipt>
compressedHTML = reReplace(renderedResults, "\>\s+\<", "> <", "ALL");    
compressedHTML = reReplace(compressedHTML, "\s{2,}", chr(13), "ALL");
compressedHTML = reReplace(compressedHTML, "\s{2,}", chr(09), "ALL");
</cfscript>

before sending the compressedHTML in a response object like this:

 {"SUCCESS":true,"DATA":  compressedHTML  }

Question
If I know I'm sending back HTML in my data object via Ajax, is there a way to gzip the response server-side before returning it vs sending chunked? If this is at all possible? If so, can I do this inside my response object or would I have to send back "pure" HTML?

Thanks!

EDIT:
Found this on setting a 'web.config' for dynamic compression - doesn't seem to work

EDIT2: Found thi snippet and am playing with it, although I'm not sure this will work.

<cfscript>
compressedHTML = reReplace(renderedResults, "\>\s+\<", "> <", "ALL");
compressedHTML = reReplace(compressedHTML, "\s{2,}", chr(13), "ALL");
compressedHTML = reReplace(compressedHTML, "\s{2,}", chr(09), "ALL");

if ( cgi.HTTP_ACCEPT_ENCODING contains "gzip" AND not showRaw ){
    cfheader name="Content-Encoding" value="gzip";

    bos = createObject("java","java.io.ByteArrayOutputStream").init();
    gzipStream = createObject("java","java.util.zip.GZIPOutputStream");
    gzipStream.init(bos);
    gzipStream.write(compressedHTML.getBytes("utf-8"));
    gzipStream.close();
    bos.flush();
    bos.close();
    encoder = createObject("java","sun.misc.
    outStr= encoder.encode(bos.toByteArray());

    compressedHTML = toString(bos.toByteArray());
    } 
</cfscript>

Probably need to try this on the response object and not the compressedTHML variable

© Stack Overflow or respective owner

Related posts about jQuery

Related posts about AJAX