如何在Coldfusion中压缩大型Ajax响应(仅适用于HTML) ?

时间:2022-06-01 21:09:24

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

我在前端运行Coldfusion8和jquery/jquery-mobile。

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

我正在尝试使用Ajax搜索引擎,试图在数据量和客户端处理时间之间找到最佳的平衡。

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.

目前,我的AJAX搜索返回40k (jqm增强标记),这避免了任何客户端增强。通过这种方式,我可以避免页面延迟2-3秒,而JQM可以增强搜索结果中的所有元素。

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:

我好奇的是,我是否可以从Coldfusion发送gzip Ajax响应。如果我现在检查搜索的标题,我有:

    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.

所以,我的请求会接受gzip,压缩,但是我要返回chunked。

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

我在cfsavecontent(称为compressedHTML)中生成AJAX响应,并运行此响应来消除空白。

<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:

在将压缩的html发送到这样的响应对象之前:

 {"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?

如果我知道我正在通过Ajax在我的数据对象中发送HTML,是否有一种方法可以在返回响应服务器端之前对其进行gzip压缩?如果有可能的话?如果是,我可以在我的响应对象中这样做,还是必须返回“纯”HTML?

Thanks!

谢谢!

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

编辑:在设置“web”时发现这个。配置'为动态压缩-似乎不工作

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

可能需要在响应对象而不是compressedTHML变量上尝试此操作

1 个解决方案

#1


3  

Ok. Got it to work.

好的。找到了工作。

I'm using this from the CFLib like so:

我是这样使用CFLib的:

<cfscript>
    // remove whitespace
    compressedHTML = reReplace(renderedResults, "\>\s+\<", "> <", "ALL");
    compressedHTML = reReplace(compressedHTML, "\s{2,}", chr(13), "ALL");
    compressedHTML = reReplace(compressedHTML, "\s{2,}", chr(09), "ALL");
    // gzip
    compressedHTML = gzip(compressedHTML);
</cfscript>
<!--- modify header --->
<cfheader name="Content-Encoding" value="gzip">
<cfheader name="Content-Length" value="#len(compressedHTML)#" >
<!--- return cfcontent with reset="no", so I'm not disrupting the Ajax request --->
<cfcontent reset="no" variable="#compressedHTML#" />
<cfreturn  />

You also need to make sure to set the return variables for the function which contains the above to binary and the Ajax request must use returntype="html". At least that's how I got it to work.

还需要确保将包含上述内容的函数的返回变量设置为二进制,Ajax请求必须使用returntype="html"。至少我就是这样让它工作的。

Seems to work nice and my Ajax requests went from 50-60k enhanced markup down to 1-2k. Nice on mobile :-)

看起来效果不错,我的Ajax请求从50-60k提高到1-2k。好的移动:-)

EDIT:
If you are having problems with special characters not being displayed correctly, try setting

编辑:如果特殊字符显示不正确,请尝试设置

 <cfheader name="Content-Type" value="text/html; charset=ISO-8859-1">

before returning cfcontent. I don't know if this is better than UTF-8, but it work for German äöüß, which I was mising in my Ajax response.

cfcontent返回之前。我不知道这是比utf - 8,但是它为德国aouß工作,我是管理信息系统在我的Ajax响应。

#1


3  

Ok. Got it to work.

好的。找到了工作。

I'm using this from the CFLib like so:

我是这样使用CFLib的:

<cfscript>
    // remove whitespace
    compressedHTML = reReplace(renderedResults, "\>\s+\<", "> <", "ALL");
    compressedHTML = reReplace(compressedHTML, "\s{2,}", chr(13), "ALL");
    compressedHTML = reReplace(compressedHTML, "\s{2,}", chr(09), "ALL");
    // gzip
    compressedHTML = gzip(compressedHTML);
</cfscript>
<!--- modify header --->
<cfheader name="Content-Encoding" value="gzip">
<cfheader name="Content-Length" value="#len(compressedHTML)#" >
<!--- return cfcontent with reset="no", so I'm not disrupting the Ajax request --->
<cfcontent reset="no" variable="#compressedHTML#" />
<cfreturn  />

You also need to make sure to set the return variables for the function which contains the above to binary and the Ajax request must use returntype="html". At least that's how I got it to work.

还需要确保将包含上述内容的函数的返回变量设置为二进制,Ajax请求必须使用returntype="html"。至少我就是这样让它工作的。

Seems to work nice and my Ajax requests went from 50-60k enhanced markup down to 1-2k. Nice on mobile :-)

看起来效果不错,我的Ajax请求从50-60k提高到1-2k。好的移动:-)

EDIT:
If you are having problems with special characters not being displayed correctly, try setting

编辑:如果特殊字符显示不正确,请尝试设置

 <cfheader name="Content-Type" value="text/html; charset=ISO-8859-1">

before returning cfcontent. I don't know if this is better than UTF-8, but it work for German äöüß, which I was mising in my Ajax response.

cfcontent返回之前。我不知道这是比utf - 8,但是它为德国aouß工作,我是管理信息系统在我的Ajax响应。