PDA

View Full Version : Gzip All CF pages



ViperLord
August 19th, 2009, 06:25 PM
Here's a method for enabling gzip compression on pages served through your application.cfc

Expanding on http://www.andyscott.id.au/index.cfm/2006/9/12/Proof-of-Concept:



<cffunction name="onRequestEnd" returnType="void" output="true">
<cfargument name="thePage" type="string" required="true">
<cfif cgi.HTTP_ACCEPT_ENCODING contains "gzip">
<cfscript>
pageOut = getPageContext().getCFOutput().getString();
fileOut = createobject("java", "java.io.ByteArrayOutputStream").init();
out = createobject("java","java.util.zip.GZIPOutputStream").init(fileOut);
out.write(variables.pageOut.getBytes(), 0, len(variables.pageOut.getBytes()));
out.finish();
out.close();
</cfscript>
<cfheader name="Content-Encoding" value="gzip">
<cfcontent type="text/html" reset="true" variable="#fileOut.toByteArray()#">
</cfif>
</cffunction>


This will compress pages before they are sent to the browser (if the browser supports it), resulting in smaller/faster pages.

If you are using CF to serve up images, XLS, other or binary filetypes, you will probably want to add something to the cfif to prevent gzip.

Support also suggested: http://cfspaghetti.blogspot.com/2007/07/how-to-gzip-compress-coldfusion-8.html, but I think my variation may be cleaner.