As some of you may or may not noticed, my site was down earlier for maintenance (more on that later). None of the ColdFusion pages on my site were available, no matter what URLs you used. Creating a page like this is insanely easy when you are using ColdFusion's Application.cfc component. If you have ever examined the application event methods available in the Application.cfc, you will notice that several of them return a boolean value. This value, while tiny, plays a huge part in the page processing. Anytime that one of the event methods returns false, it kills the rest of the page / application processing.
Using this information, we can easily build our "Down For Maintenance" page directly into the architecture of the Application.cfc component. Since we want to display this page for every request, it feels natural to put this functionality in the OnRequestStart() event method:
<cffunction name="OnRequestStart" access="public" returntype="boolean" output="true" hint="Fires at the beginning of a page requested before the requested template is processed."> <!--- Define arguments. ---> <cfargument name="TargetPage" type="string" required="true" hint="The template that was requested by the user." /> <!--- Define the local scope. ---> <cfset var LOCAL = StructNew() /> <!--- Set header code. ---> <cfheader statuscode="503" statustext="Service Temporarily Unavailable" /> <!--- Set retry time. ---> <cfheader name="retry-after" value="3600" /> <h1> Down For Maintenance </h1> <p> The web site is current down for maintenance and will be back up shortly. Sorry for the inconvenience. </p> <!--- By returning false, the rest of the page rendering will hault. ---> <cfreturn false /> </cffunction>
When creating a Down For Maintenance page, we have to be concerned with more than just the visual aspects; we also want to make sure that search engines such as Google and Yahoo respect that fact that our site is down and don't just assume that our site no longer has content! To do this, we explicitly set the Status Code of the page header to be:
503 - Service Temporarily Unavailable
This will alert the search engine bots and spiders that the site is down and to ignore the pages. But, we also want to take it one step further - we want to reassure the search engine bots that the site will be back up and that the bots should re-index the site shortly. To do this, we set another header value, retry-after. This returns the number of seconds (3,600 in our case) the search engine bots should wait before trying to re-index the site. While I am not sure about this, I have read that if you do NOT return a retry-after header value, the client (browser / bot) should treat this as a 500 - Internal Server Error, so be sure to set this properly!
After our header values, we output our HTML content and then return false. Returning "false" here is a huge point because by returning false, the ColdFusion application server knows not to even bother calling the OnRequest() event method which would have processed the requested template.
If you are using ColdFusion's Application.cfm template instead of Application.cfc, the same thing can be done, but with slightly less elegance. Instead of using any event methods (which don't exist in Application.cfc), you could simply put the header and output code directly into the Applicaiton.cfm:
<!--- Set header code. ---> <cfheader statuscode="503" statustext="Service Temporarily Unavailable" /> <!--- Set retry time. ---> <cfheader name="retry-after" value="3600" /> <h1> Down For Maintenance </h1> <p> The web site is current down for maintenance and will be back up shortly. Sorry for the inconvenience. </p> <!--- Abort page. ---> <cfabort />
Then, instead of returning false, simply stop the page processing by executing a ColdFusion CFAbort tag.
While we put our output and header logic directly into our Application templates, there is nothing to say that we couldn't wrap this functionality into a template of its own which could then be included in the Application templates.
Want to use code from this post? Check out the license.