Skip to content

Instantly share code, notes, and snippets.

@abcarroll
Forked from attaboy/gist:1346280
Last active August 29, 2015 14:18
Show Gist options
  • Save abcarroll/8c96eb0cbfce280e6bf6 to your computer and use it in GitHub Desktop.
Save abcarroll/8c96eb0cbfce280e6bf6 to your computer and use it in GitHub Desktop.
/*
* This clears the LESS.js cache, since it aggressively caches @import statements.
* Credit: Attaboy, https://gist.github.com/attaboy/1346280
* Additional key checks and debugging output by A.B. Carroll. (http://github.com/nezzario)
* License: Assumed MIT/BSD-like license, please give credit where credit is due.
*/
less.env = 'development';
console.log("If you are seeing this in a production environment you are likely doing something wrong or forgot to remove the destroyLessCache() script.");
function destroyLessCache(pathToCss) { // e.g. '/css/' or '/stylesheets/'
if (!window.localStorage || !less || less.env !== 'development') {
console.log('destroyLessCache(): Not destroying cache. Either no local storage, LESS not loaded, or less.env not set to development');
return;
}
var host = window.location.host;
var protocol = window.location.protocol;
var keyPrefix = protocol + '//' + host + pathToCss;
for (var key in window.localStorage) {
if (key.indexOf(keyPrefix) === 0 && (key.indexOf('.less', key.length - 5) !== -1 || key.indexOf('.less:timestamp', key.length - 15) !== -1)) {
console.log('Deleted LESS Cache Key: ' + key);
delete window.localStorage[key];
}
}
}
// You MUST put any path that contains LESS files that are being compiled here as separate function calls (or just add '/' if you feel it's safe).
destroyLessCache('/css/');
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment