Results 1 to 1 of 1

Thread: Gzip All CF pages

  1. #1
    ViperLord Guest

    Default Gzip All CF pages

    Here's a method for enabling gzip compression on pages served through your application.cfc

    Expanding on http://www.andyscott.id.au/index.cfm...oof-of-Concept:

    Code:
    <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...dfusion-8.html, but I think my variation may be cleaner.
    Last edited by ViperLord; August 19th, 2009 at 06:59 PM.

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •