Skip to content

Instantly share code, notes, and snippets.

@akaita
Created Nov 19, 2019
Embed
What would you like to do?
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