Created
November 26, 2012 09:40
-
-
Save nikcorg/4147414 to your computer and use it in GitHub Desktop.
Cache busting helper
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
#!/bin/bash | |
PROJECTHOME=$(dirname $(dirname $0)) | |
JSSOURCEDIR="$PROJECTHOME/public/js" | |
VERSIONFILE="$JSSOURCEDIR/version.js" | |
VERSION=$(git describe --all --long | tr "-" " " | cut -d " " -f 3) | |
if [ ! -d "$PROJECTHOME" -o ! -d "$JSSOURCEDIR" ]; | |
then | |
echo "Unable to resolve PROJECTHOME or JSSOURCEDIR" | |
exit 1 | |
fi | |
if [ ! -f $VERSIONFILE ] | |
then | |
touch $VERSIONFILE | |
fi | |
cat > $VERSIONFILE <<JSSOURCE | |
var getVersion = (function () { | |
var version = "$VERSION"; | |
return function () { | |
return version; | |
} | |
}()); | |
JSSOURCE |
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
var version = (function () { | |
"use strict"; | |
if ("getVersion" in window) { | |
return getVersion(); | |
} | |
return Math.random() * 9999; | |
}()); | |
requirejs.config({ | |
urlArgs: "v=" + version | |
}); | |
define(function () { | |
"use strict"; | |
// app init code here | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
This is a script I use to eliminate caching problems, without reverting to random numbers. I place this in <proj.root>/scripts and it generates a file called version.js in <proj.root>/public/js, which is included in the head of my index.html. I use the version in the urlArgs config for requirejs.