« Macrochats Are Back! | Main | New LiveDocs Features »

January 12, 2005

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:

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>

Posted by cantrell at January 12, 2005 12:39 PM

Comments

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

Posted by: ross at January 12, 2005 12:56 PM

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.

Posted by: Raymond Camden at January 12, 2005 1:23 PM

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

Posted by: ronaldo at January 12, 2005 1:52 PM

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.

Posted by: mac user at January 12, 2005 2:21 PM

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.

Posted by: Alex Sherwood at January 12, 2005 2:27 PM

two things I'll be interested in/trying out

1) Raymond Camden's concern on preformatted text
2) any effects on encoded (encrypted) CFM files

we're using zillions of UI custom tags, each with their own whitespace - it all adds up...

cheers for the tip
barry.b

Posted by: barry.b at January 12, 2005 6:06 PM

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)

cheers
barry.b

Posted by: barry.b at January 12, 2005 6:55 PM

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

Regards,

Posted by: Marcos Placoná at January 13, 2005 7:38 AM

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.

Posted by: Erki Esken at January 13, 2005 11:15 AM

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

Posted by: Wizard at January 16, 2005 6:09 PM

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

Posted by: Kelly Tetterton at January 17, 2005 11:33 AM

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?

regards

Steve

Posted by: Steve Powell at January 19, 2005 10:08 AM

1) There is a custom tag here: http://www.servletsuite.com/jsp.htm
- Optimize taglib. With this taglib you can remove the whitespaces within
the any region of your code

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

Posted by: Dmitry Namiot at January 21, 2005 2:45 AM


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 buffer
getPageContext().getOut().clearBuffer();

// tidy up
pageContent = reReplace( pageContent, ">[[:space:]]+#chr( 13 )#<", "all" );

// send our cleaned content to the browser
writeOutput( pageContent );
getPageContext().getOut().flush();

// job done!

</cfscript>

Chuck this into a onRequestEnd.cfm and you're laughing.

Have fun.

Jon Alsbury
Senior Web Services Developer
London Metropolitan University

Posted by: Jon Alsbury at January 26, 2005 8:31 AM

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

Posted by: Sam at February 17, 2005 8:14 PM

Are there any variations for the install w/ CF7?

Posted by: Mike Britton at April 28, 2005 6:30 PM

Does TrimFilter work with CFMX 7?

Thanks,
Richard

Posted by: Richard at August 3, 2005 1:39 PM

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/anevent

Should:

trimFilter
*.cfm


Be changed to:

trimFilter
*.cfm*


With a change in "*.cfm*"?

Sami

Posted by: Sami Hoda at September 9, 2005 9:39 AM

Small bug in the code that Dmitry submitted.. the reReplace should be like this, otherwise it won't work.

reReplace( pageContent, ">[[:space:]]+#chr( 13 )#

Posted by: Tjarko at September 12, 2005 1:52 AM

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

Posted by: Asala Kiolu at November 3, 2005 10:51 PM




Remember Me?

(you may use HTML tags for style)