Eliminating Whitespace in ColdFusion

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.

View Comments

  1. Ethan Cane says:

    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

  2. Marcel says:

    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.

  3. Simon says:

    @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.

  4. petron says:

    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!

  5. Simon says:

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

  6. Shawn says:

    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!

  7. Alex says:

    Nice. Works like a charm.

    Thanks!

  8. Noname says:

    To remove empty lines from an xml:

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

  9. Datico says:

    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).

  10. Datico says:

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

  11. Grant says:

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

blog comments powered by Disqus