Skip to content

Instantly share code, notes, and snippets.

@akaita
Created November 19, 2019 14:12
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save akaita/63cd58a1c91b81313cb50316a22e899c to your computer and use it in GitHub Desktop.
Save akaita/63cd58a1c91b81313cb50316a22e899c to your computer and use it in GitHub Desktop.
Custom tag for ColdFusion implementing cache busting
<!---
Goal: implement timestamp-based cache busting
For example: instead of loading a javascript file from "http://www.example.com/myscript.js", it'll load it from "http://www.example.com/myscript.js?20191101160030"
Usage examples:
- add as global tag and: <CF_js_script src="/js/myscript.js">
- <cfimport prefix="akaita" taglib="customTags"> and then <akaita:js_script src="/js/myscript.js"></akaita:js_script>
--->
<CFSETTING ENABLECFOUTPUTONLY="Yes">
<CFIF thisTag.executionMode IS "start">
<CFSET scriptFile = expandPath(attributes.src)>
<CFIF fileExists(scriptFile)>
<CFSET fileInfo = getFileInfo(scriptFile)>
<CFSET timeStamp = dateFormat(fileInfo.lastModified, "yyyymmdd")
& timeFormat(fileInfo.lastModified, "HHmmss")>
<CFELSE>
<CFOUTPUT>File #attributes.src# not found</CFOUTPUT>
<CFABORT>
</CFIF>
<CFOUTPUT>
<SCRIPT type="text/javascript" src="#attributes.src#?ver=#timestamp#"></SCRIPT>
</CFOUTPUT>
</CFIF>
<CFSETTING ENABLECFOUTPUTONLY="no">
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment