Using ColdFusion's onRequestStart Method Adds Whitespace to Templates

After banging my head against my desk (which is glass by the way!) I finally figured out how to resolve an issue that may affect others out there using the ColdFusion onRequestStart method in their Application.cfc.

What Happened?
I was attempting to build a dynamic page that returned only text so that I could consume it in Flash using some ActionScript magic. After getting some errors from Flash, I decided to check the ColdFusion template I was trying to access. On the surface, everything looked fine. However, once I did a "View Source", I noticed that the ColdFusion page had some extra whitespace and carriage returns in it. This is not good, because ActionScript wants to read the entire document, whitespace and all!

So off I went, trying <cfsetting enablecfoutputonly="true">. That didn't do a thing! So I threw another CF bomb at it by wrapping everything in <cfprocessingdirective suppresswhitespace="true"></cfprocessingdirective>. Nothing. I know, you're thinking <cfsilent> aren't you? It was already there my friends, and nada. I'm telling you I removed every extra space in that template's code and didn't get any closer to getting rid of the extra space.

And that's when it hit me ... check the Application.cfc and see if there's anything in there that might be generating it. So off I went again, going through each line of code in my Application.cfc. I decided to start commenting out some of the methods to see what, if any, impact it might have on the template. When I worked my way down to onRequestStart() and commented it out ... everything worked as it should! Well, that's great! BUT ... I still wanted to use my onRequestStart() code.

So to start with, let's take a look at some generic onRequestStart code:


    <cffunction name="onRequestStart">
    
        <cfargument name="request" required="true" />
    
        <!--- re-start application from URL with ?init --->
        <cfif isDefined("url.init")>
            <cfset onApplicationStart() />
        </cfif>

    </cffunction>

In order to still use the onRequestStart method, it turns out all I have to do is remove any and all white space and carriage returns in the onRequestStart method. So the revised code would look something like this:

<cffunction name="onRequestStart" access="public" output="true" hint="Fires when prior to page processing."><cfargument name="request" required="true" /><!--- re-start application from URL with ?init ---><cfif isDefined("url.init")><cfset onApplicationStart() /></cfif></cffunction>

Hopefully this saves someone else from a sore forehead.

Comments
There are 2 other options. 1. set output="false" for the onRequestStart method, or 2. put your code within a <cfsilent> block.
# Posted By Brian Love | 3/31/09 11:58 AM
True, the output="false" would work as well. However, the <cfsilent> trick did nothing for me.

Another trick sent to me via twitter from Bradley Moore was to place <cfcontent reset="true" /><cfreturn true /> at the end. However, the trick is that they have to be on the same line without any whitespace between them.
# Posted By Stephen Withington | 3/31/09 12:08 PM
<cffunction name="onRequestStart" output="false">

all your functions should be set to output="false" unless you really need to output something without using RETURN.

# Posted By Jorrit | 4/1/09 3:12 AM
@Jorrit,
I agree and am glad you brought that up. Thanks!

# Posted By Stephen Withington | 4/1/09 7:27 AM
This summon accentuation can be totally distorted or to an awesome degree trapped. As you wind up being more OK with database programming, you will understand how to improve your SELECT enunciations, at last influencing your database to do however much work as could be ordinary and not crippling your programming vernacular of choice.
# Posted By Black Leather Jackets | 1/30/18 11:45 PM
This series of replica rolex watches http://www.swisswatchjust.co.uk is not only a re-engraved classic watch, but omega replica the traditional aesthetic features replica watchesand contemporary
http://www.ukreplicawatch.co.uk watchmaking process integration
# Posted By sdgyer | 3/25/18 10:36 PM

© 2019, Stephen J. Withington, Jr.  |  BlogCFC was created by Raymond Camden – Version 5.9.004

Creative Commons License  |  This work is licensed under a Creative Commons Attribution 3.0 Unported License.  |  Hosted by Hostek.com