Created
November 19, 2019 14:12
-
-
Save akaita/63cd58a1c91b81313cb50316a22e899c to your computer and use it in GitHub Desktop.
Custom tag for ColdFusion implementing cache busting
This file contains hidden or 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