Eliminate ColdFusion Whitespace Once and For All

Since I’m at Macworld this week, and consequently don’t have a lot of time to
put into my weblog, I’m going to be lazy, and reprint a comment that was sent to
me by Jon Alsbury. It was submitted in response to a post entitled Controlling
Whitespace in ColdFusion
. John writes:

The most effective (and easy to implement) technique for reducing whitespace in
CFMX generated pages I have discovered so far is to set up a simple servlet filter
to intercept the response in order to strip out whitespace before it is returned
to the client. The filer I’ve been using for this is called Trim Filter and can
be downloaded here:


Setup is easy: simply download trimflt.jar from the above URL, drop it into your
‘cfusionmx/lib’ directory. Add the following to ‘cfusionmx/wwwroot/WEB-INF/web.xml’:



20 Responses to Eliminate ColdFusion Whitespace Once and For All

  1. ross says:


  2. I could test this – but wanted to ask first – does this filter correctly handle pre formatted text? If not – then it isn’t safe to use.

  3. ronaldo says:

    It’s very interesting but my scripts (charsets western) are all corrupts. It only work on unicode. :/

  4. mac user says:

    Macromedia is at MacWorld this year? Good to see, they don’t seem to focus much on their Mac products as much as they used to.

  5. Alex Sherwood says:

    Kind of sad that you have to install a 3rd party filter to achieve a feature that is supposed to be native to CFML. And a simple feature, at that.Good thing MACR is spending lots of money on things like SMS gateways for the next version of CF.

  6. barry.b says:

    two things I’ll be interested in/trying out1) Raymond Camden’s concern on preformatted text2) any effects on encoded (encrypted) CFM fileswe’re using zillions of UI custom tags, each with their own whitespace – it all adds up…cheers for the tipbarry.b

  7. barry.b says:

    well, as for preformatted text:each line is preserved but any whitespace (tab, etc) at the start of the chars on each line is removed.this means that the SQL debugging in CF pushes the queries to the left instead of indented (ie: no big deal for us here but would cheese any blog using pre tags to display indented code….sorry Ray)I won’t have a chance to do any performance testing yet but I’ve got a hunch it’s making the page run a touch slower in cases where there are 50+ files “cfimport” ed into a page.anyone noticed this?(PS: have to come back lateer to see how the the encoded pages fare)cheersbarry.b

  8. The problem is that all the charsets of my scripts are corrupt.Regards,

  9. Erki Esken says:

    The best CF whitespace elimination technique is using cfsetting enablecfoutputonly=”yes” in Application.cfm and then using cfoutput where you want something to show. Or for big blocks set enablecfoutputonly=”no” at the beginning and “yes” at the end. This way you’ll have minimal amount of whitespace.

  10. Wizard says:

    Hello!Trim filter not worked and in Unicode.All not ENG chars = ????

  11. Kelly Tetterton says:

    Does anyone have any sense of how this does or doesn’t affect server performance under load?

  12. Steve Powell says:

    Been running this on one of our dev boxes for almost a week now and not a problem in sight. Server performance doesn’t seem to be affected though this is only a dev box. Having said that it runs apps for 7 yes seven different developers and has about 14 other apps on it that are in testing/user acceptance.If it continues to be problem free I’m going to push it onto the live boxes. I’m getting at least a 10% file size saving.Now I just need to sort out one of those Zip thingy wotsits pligins for IIS. Anyone have any reccomendations?regardsSteve

  13. 1) There is a custom tag here: http://www.servletsuite.com/jsp.htm– Optimize taglib. With this taglib you can remove the whitespaces withinthe any region of your code2) As per charsets: filter uses a default encoding. In other words filter does not define own encoding and uses it from the parent. There is a lot of filters here: http://www.servletsuite.com/servlets.htm including components that will set the encoding for your stuff.3) any suggestions are welcome: http://www.servletsuite.com

  14. Jon Alsbury says:

    Glad so many folks have found this tip useful. One other side effect is that the servlet filter will screw up RDS, but most people won’t be running RDS on production servers anyway, will they?Anyhow, here’s a slightly less brutal solution for folks worried about messing up their <PRE> blocks or character encoding:<cfscript>// copy the current contents of out (which is what cfmx will send to the browser at the end of processing)pageContent = getPageContext().getOut().getString();// now we have a copy, clear the out buffergetPageContext().getOut().clearBuffer();// tidy uppageContent = reReplace( pageContent, “>[[:space:]]+#chr( 13 )#<“, “all” );// send our cleaned content to the browserwriteOutput( pageContent );getPageContext().getOut().flush();// job done!</cfscript>Chuck this into a onRequestEnd.cfm and you’re laughing.Have fun.Jon AlsburySenior Web Services DeveloperLondon Metropolitan University

  15. Sam says:

    Another tip from me :)Before installing CF, set your server/computer date 3 years later and install CF in trial mode.After installation, get date back to original.UPPPS you have a working CF version for next 3 years.WAAAAV!!! 🙂

  16. Mike Britton says:

    Are there any variations for the install w/ CF7?

  17. Richard says:

    Does TrimFilter work with CFMX 7?Thanks,Richard

  18. Sami Hoda says:

    Not sure if it works with CF7. Tried it, didnt do anything. But maybe its because I use SES URLs.For example, xyz.com/index.cfm/go/aneventShould:trimFilter*.cfmBe changed to:trimFilter*.cfm*With a change in “*.cfm*”?Sami

  19. Tjarko says:

    Small bug in the code that Dmitry submitted.. the reReplace should be like this, otherwise it won’t work.reReplace( pageContent, “>[[:space:]]+#chr( 13 )#

  20. Asala Kiolu says:

    Adedeji Olowe has another method that is similar to Jon Alsbury’s method. He built his strategy on a function from cflib. You can check it out here http://www.dejiolowe.com/pinkjacket/blog/index.cfm?mode=entry&entry=4D631010-3048-2C4E-B8472B63686FB32D