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