How can I generate static HTML from a dynamic ColdFusion page?
One very powerful caching technique is to run your ColdFusion code to generate a dynamic web page, and then write the contents of the dynamic page to a static HTML file. The static page can then be loaded time and time again without the expense of having to rebuild the page.
To acomplish this task, ypou can use use a combination of the ColdFusion <cfsavecontent> and <cffile> tags, or the ColdFusion scheduling engine to write the HTML generated from running a dynamic page to a static HTML file.
The first method to generate and static documents from dynamic content is to use a combination of the ColdFusion <cfsavecontent> and <cffile> tags. The <cfsavecontent> tag stores the generated output from a block of ColdFusion and HTML code into a varaible. So one you have that varaible, it is a simple matter of using the <cffile> tag to write the data to a static HTML file. The following code shows a sample of this technique:
<cfsavecontent variable="cachedOutput">
<html>
<head>
<title>Cached File Example</title>
</head>
<body>
<h3>Cached File Example</h3>
<cfloop index="loopOn" from="1" to="10">
<cfoutput>The loop is on: #loopOn#<br></cfoutput>
</cfloop>
</body>
</html>
</cfsavecontent>
<!--- Write cached contents (HTML) to file --->
<cffile action="write"
file="C:\temp\cachedFile.html"
output="#cachedOutput#">
A second way to generate static documents from dynamic content is to use the ColdFusion scheduling engine. Along with the ability to generate static documents from dynamic content, using the ColdFusion scheduling engine gives you the added ability to automatically update your static documents on whatever schedule you may require.
There are two main options for working with the ColdFusion scheduling engine. The first option is by way of the ColdFusion Administrator, and the second is by using the <cfschedule> tag. For our example, we will use this second method.
The real magic of using the ColdFusion scheduling engine to generate static documents from dynamic content lies in the publish attribute. If this attribute is set to yes, then the results of the page the scheduled task runs will be saved to disk (as per the file and path attributes). Beyond this, we are simply telling the ColdFusion scheduling engine what page to run, and how often the page should be ran. Some sample code using this technique would look as follows:
task = "createStaticPage"
operation = "httpRequest"
file = "cachedFile.html"
path = "C:\inetpub\wwwroot\"
startDate = "2/13/2006"
startTime = "12:00 PM"
url = "http://127.0.0.1/createPage.cfm"
publish = "yes"
interval = "3600"
resolveURL = "yes">
This question was written by Jeremy Petersen.
It was last updated on February 13, 2006 at 11:27:30 AM EST.
CFML Referenced
<cffile>
<cfsavecontent>
<cfoutput>
<cfschedule>
<cfloop>
Categories
Comments
Comment made by Mike Potter on February 13, 2006 at 12:38 PM
Jeremy: I wrote a similar script for PHP which I thought may be of interest to you. Essentially, you configure your webserver to call the script when the webserver encouters a 404 error. The script then creates the HTML page, and users get the static page, as opposed to the 404 message. An easy way to find out which pages are being accessed, as well as enabling you to create a static site using dynamic content. When you want to update the page, just delete all the static content. The PHP tutorial is at http://www.zend.com/zend/tut/tutorial-potter.php
Mike
Comment made by Sam on March 10, 2006 at 5:00 PM
cfschedule is a great tag to use, but unfortunately it does not support ssl and it can only execute urls. So, since my company uses ssl, the tag is of no worth. We'd like to execute a cfmx page on a schedule, but cfschedule does not support https connections and cannot execute server side files (only can execute urls). So, we are still considering alternative options to the dilemma.