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:

- Restart the ColdFusion Service
And now, when a ColdFusion page is invoked, the whitespace is suppressed and wow does it do a good job.
Tags: bandwidth, ColdFusion, HTML, servlets, tags, web.xml, whitespace




















6 comments
Comments feed for this article
Trackback link
http://www.simonwhatley.co.uk/eliminating-whitespace-in-coldfusion/trackback
26 October 2007 at 9:59 am
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
26 October 2007 at 10:11 am
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.
26 October 2007 at 12:59 pm
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.
26 October 2007 at 1:04 pm
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!
26 October 2007 at 1:23 pm
Simon
@Petron - Thanks for noticing the error, I’ve corrected the image.
13 June 2008 at 9:49 am
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!