Whatterz


Eliminating Whitespace in ColdFusion

by Simon. Average Reading Time: about 2 minutes.

ColdFusion has never satisfactorily removed whitespace from generated content, however, removing this whitespace can dramatically improve your website’s performance. Take a 100KB page for example. If 20% of the page is made up of whitespace, that is 20% that is unneccessary and 20% bandwidth cost that can be saved.

Currently there are two main ways a ColdFusion developer can prevent whitespace; via the ColdFusion Administrator and by including certain tags in their code:

1. ColdFusion Administrator

Under the Server Settings > Settings menu item there is a checkbox called ‘Enable Whitespace Management’. This checkbox should always be checked. According to the description, checking this checkbox “reduces the file size of the pages that ColdFusion returns to the browser by removing many of the extra spaces, tabs, and carriage returns that ColdFusion might otherwise persist from the CFML source file.” I am yet to be convinced, but it is worthwhile enabling it.

2. ColdFusion Tags

<cfsetting enablecfoutputonly="true"></cfsetting>

The cfsetting tag controls aspects of page processing, such as the output of HTML code in pages (inside and outside the cfsetting tag body). If enablecfoutputonly is set to true, HTML output is blocked if it is not wrapped in a cfoutput tag. Therefore, this tag ‘can’ be used to minimise the amount of generated whitespace.

<cfsilent></cfsilent>

The cfsilent tag supresses output produced by CFML within a tag’s scope. Therefore, you simply need to wrap the tag around anything you don’t want to output to the browser. As it does not return anything from with in it, so you have to be a little be careful when debugging.

<cfprocessingdirective supresswhitespace="true"></cfprocessingdirective>

One of the cfprocessingdirective’s purposes is to remove excess whitespace from ColdFusion generated content in the tag body. However, it does not affect any whitespace in HTML code. You can nest the tags and toggle supresswhitespace on and off, not that I ever identify a good reason to do so.

But there is a little known third way, using a servlet filter called Trim Filter.

Servlet filters are tools available to web application developers. They are designed to be able to manipulate a request and response (or both) that is sent to a web application.

Trim Filter lets you decrease the size of file your server will send to all clients. The filter removes extra spaces and line breaks from outputted documents. This can be especially useful for WAP/WML developers working with mobile devices, where the size for transferred documents is limited.

Setting up the servlet filter in ColdFusion

Using the Trim Filter servlet filter found at the following URL:

http://www.servletsuite.com/servlets/trimflt.htm

  • Download the trimflt.jar
  • Save trimflt.jar in the WEB-INF/lib folder in the ColdFusion Server wwwroot.
  • Edit the web.xml file in the WEB-INF folder in the ColdFusion Server wwwroot with the following code:
<filter-mapping>
	<filter-name>trimFilter</filter-name>
	<url-pattern>*.cfm</url-pattern>
</filter-mapping>
 
<filter>
	<filter-name>trimFilter</filter-name>
	<filter-class>com.cj.trim.trimFilter</filter-class>
</filter>
  • Restart the ColdFusion Service

And now, when a ColdFusion page is invoked, the whitespace is suppressed and wow does it do a good job.

This article has been tagged

, , , , , , , , , , , , , , , , ,

Other articles I recommend

Poor Man's HTTP Compression with ColdFusion

Almost every web application will benefit from the compression of content. A compression filter optimises the size of the content that is sent from a webserver to a web browser via the Internet. Since generating content and serving pages via the World Wide Web is the core behind web applications, it is simple components that aid these processes that are incredibly useful. This is where servlet filters come into play.

Parsing Twitter Usernames, Hashtags and URLs with ColdFusion

Some time ago, well almost a year ago actually, I posted an article called Parsing Twitter Usernames, Hashtags and URLs with JavaScript. From that article, it became immediately apparent that this was an issue many people were confronting and one that required an answer. Now, belatedly, it is the turn of ColdFusion to get the Twitter love.

Parsing Twitter Usernames, Hashtags and URLs with JavaScript

As part of an AIR project that I have been working on with my good friend Rob, we came across the need to parse a number of URLs within the text of a Twitter post. This may not sound too easy at first, but thanks to the prototype property available on JavaScript objects, our task was a relatively simple one.

  • http://blog.xmlstandards.org/ Ethan Cane

    Hi Simon,

    I have used the trim method you mentioned before and it does indeed work wonderfully, however there is a side effect to this approach which I believe affects connections made via RDS. That is the only point to be wary of. As far as I can remember it will perform compression (white space management) against templates you load into Eclipse/Dreamweaver/(any other RDS capable client) rendering the content either un-editable or garbled.

    Hopefully since most savvy developers won’t be using RDS (insecure) it won’t pose a huge problem and is best left for production setups anyway.

    Hope that helps.

    Ethan Cane
    Web Developer

  • http://www.yellowcat.nl Marcel

    Do you know how trimFilter behaves with Javascript code. A lot of these filters and solutions are breaking your javascript code, becuase they remove line breaks where they are needed.

  • http://www.simonwhatley.co.uk Simon

    @Marcel – I haven’t come across a problem with javascript. If the javascript statements are correctly terminated etc, they should be rendered as intended. The servlet filter seems to be pretty clever at only removing what is not needed, but as with everything, give it a thorough test as my environment won’t be the same as yours.

  • petron

    This works awesome!

    However make sure you put *.cfm instead of *.jsp in the url-pattern if you want it to work on your CF pages.

    Thanks!

  • http://www.simonwhatley.co.uk Simon

    @Petron – Thanks for noticing the error, I’ve corrected the image.

  • http://www.kloppy.com Shawn

    Thanks, for the info saved me about a trimmed about a 1/6th off the download. I was going to go in manually and try to use the traditional methods you mentioned but this is much easier.

    Thanks!

  • Alex

    Nice. Works like a charm.

    Thanks!

  • Noname

    To remove empty lines from an xml:

    ReReplace(xml, “[\s]+[#Chr(13)##Chr(10)#]+”, “#Chr(10)#”, “ALL”)

  • Datico

    Of all the CF methods you mention, I have found the following best practice to be most useful:
    1) for my entire app (put it in application.cfc onRequest() or at the top of every page or something).
    2) ALWAYS wrap my output in a cfoutput tag.
    This has simplified my development a lot and I have no white space issues any longer. The separation of concerns (code, output) has helped simplify my file structure; it is very clear where html output is happening and where it is not. I simply use a cfloop inside a cfoutput if I want to output a query. It also avoids the unpleasant issue of white-space trimmers mucking with my page and causing it to break (which happens rarely, but when it does can be a pain to fix).

  • Datico

    Oops, for 1) I meant <cfsetting enablecfoutputonly=”yes”> for my entire app

  • Grant

    <cfcontent type=”text/html; charset=ISO-8859-1″ reset=”true” /> will reset the content loaded by a page to blank… I usually put at the top of my onRequestStart() in application.cfm. Just for those who don't have the ATM to mess with a servlet install and need a quick fix.

  • http://www.facebook.com/people/Mark-Klepa/697789460 Mark Klepa

    Why do I get an error when trying this? I’m using CF9. I get something like the application is not configured to run your request.

  • http://www.simonwhatley.co.uk/ Simon Whatley

    @mark I haven’t tried the servlet filter on ColdFusion 9, but perhaps the JAR file conflicts with later versions of libraries used within ColdFusion.

  • jason

    man, i was outputting a TAB file and i kept manually removing whitespace until i got fed up with it and found this article. the first option under server settings solved this for me. now the output tab-delimeted file has no whitespace generated from code. thanks again. 

  • niccottrell

    What about the performance implications? If you have GZIP compression enabled in the webserver then some extra whitespace from ColdFusion shouldn’t be a problem. I’m currently profiling a ColdFusion server and there is a lot of overhead going to a _whitespace method call. Not sure if this is caused by whitespace in some CFC file or whether this is ColdFusion trying to remove space. Any ideas?