Skip to content

Instantly share code, notes, and snippets.

@nikcorg
Created November 26, 2012 09:40
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 nikcorg/4147414 to your computer and use it in GitHub Desktop.
Save nikcorg/4147414 to your computer and use it in GitHub Desktop.
Cache busting helper
#!/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
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
});
@nikcorg
Copy link
Author

nikcorg commented Nov 26, 2012

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment