Serving .cfm files with <script> tag

caching, coldfusion, javascript

Solution

The advantage of the technique above is that the server is generating JavaScript. That means you can use ColdFusion to perhaps get data from a database or the user's session. Perhaps you need a set of data to populate some dropdowns, depending on what someone selects. The abc.cfm in the example above can query the DB and generate some JavaScript containing the data you need.

With regards to the browser caching, the browser will cache what it's told to cache. If you send headers in the abc.cfm telling the browser not to cache the file, it'll reload it each time the page is loaded. A typical set of headers used to indicate no caching are:

<cfheader name="Cache-Control" value="no-cache, must-revalidate">
<cfheader name="Pragma"value="no-cache">
<cfheader name="Expires" value="Sat, 26 Jul 1997 05:00:00 GMT"> <!--- Date in the past --->

If you pop that at the top of your ABC.cfm, the browser ought to load it afresh each time.

The downsides of the approach are that the browser has to load the abc.cfm each time the page loads, slowing the page down. Another problem is that the abc.cfm tends to contain a mix of JS and CF code, which is not nice to maintain. An alternative would be to write all your code as a .JS file, which you let the browser cache and to use CF code to just spit out the data you need as JSON direct into the page. You then call the JS code, passing in the JSON data you printed into the page. Look at SerializeJSON for a neat way to turn CF data into JSON the client can read.

Problem

I have often seen that sometimes dynamic files are served through the `<script>` tag like below. The "abc.cfm" file contains both JavaScript and server side CF code. ``` <script type="text/javascript" src="abc.cfm"> ``` Normally JavaScript files are cached in the browser if we use a simple .js file, rather than a server .cfm file. So how will the above behave with regard to caching? Will the browser use the cached contents of JavaScript, or it will make a call every time to the CF server to fetch the contents of the code and recompile the js again? Also, what are the advantages and disadvantages of using this technique? I searched a lot but could not find a relevant good answer which explains this properly. Regards

Original source