by Christian Cantrell

 Comments (20)

Created

January 12, 2005

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:

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

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’:

<filter>
<filter-name>trimFilter</filter-name>
<filter-class>com.cj.trim.trimFilter</filter-class>
</filter>

<filter-mapping>
<filter-name>trimFilter</filter-name>
<url-pattern>*.cfm</url-pattern>
</filter-mapping>

COMMENTS

  • By ross - 12:56 PM on January 12, 2005   Reply

    HALLELUJAH – THE GODS HAVE SHINED DOWN ON US!!!

  • By Raymond Camden - 1:23 PM on January 12, 2005   Reply

    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.

  • By ronaldo - 1:52 PM on January 12, 2005   Reply

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

  • By mac user - 2:21 PM on January 12, 2005   Reply

    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.

  • By Alex Sherwood - 2:27 PM on January 12, 2005   Reply

    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.

  • By barry.b - 6:06 PM on January 12, 2005   Reply

    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

  • By barry.b - 6:55 PM on January 12, 2005   Reply

    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

  • By Marcos Placoná - 7:38 AM on January 13, 2005   Reply

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

  • By Erki Esken - 11:15 AM on January 13, 2005   Reply

    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.

  • By Wizard - 6:09 PM on January 16, 2005   Reply

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

  • By Kelly Tetterton - 11:33 AM on January 17, 2005   Reply

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

  • By Steve Powell - 10:08 AM on January 19, 2005   Reply

    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

  • By Dmitry Namiot - 2:45 AM on January 21, 2005   Reply

    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

  • By Jon Alsbury - 8:31 AM on January 26, 2005   Reply

    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

  • By Sam - 8:14 PM on February 17, 2005   Reply

    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!!! :)

  • By Mike Britton - 6:30 PM on April 28, 2005   Reply

    Are there any variations for the install w/ CF7?

  • By Richard - 1:39 PM on August 3, 2005   Reply

    Does TrimFilter work with CFMX 7?Thanks,Richard

  • By Sami Hoda - 9:39 AM on September 9, 2005   Reply

    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

  • By Tjarko - 1:52 AM on September 12, 2005   Reply

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

  • By Asala Kiolu - 10:51 PM on November 3, 2005   Reply

    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

ADD A COMMENT