Custom tag for ColdFusion implementing cache busting
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!--- | |
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