Skip to content

Instantly share code, notes, and snippets.

@CNSKnight
Last active March 27, 2017 23:34
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 CNSKnight/1109e9d7514dcd23a39c31bdd8c1450e to your computer and use it in GitHub Desktop.
Save CNSKnight/1109e9d7514dcd23a39c31bdd8c1450e to your computer and use it in GitHub Desktop.
System.js Cache-Busting Extension / how-to use script tokens across your Systemjs app - System.js doesn't provide a way to request a cache-busting version of a script in it's care. This handles it in a fairly simple way, and enables use of static tokens in dependency lists, rather cumbersome paths. I've provided some AMD module usage sampling as…
require([System.setPath('myModule')], function(mM) {
...
};
define([System.setPath('myModuleDep')], function(mM) {
...
};
import mMDep from System.setPath('myModuleDep');
// Sample Systemjs Config
// Somehow, provide a unique build identifier to System.cbv
System.cbv = '54321'; // or namspace.cbv
// you'll not be able to use alias' in these paths because they will themselves become maps
System.setPath('myModule', 'modules/MYMODULE/templates/js/myModule.source.js');
System.setPath('myModuleDep', 'modules/MYMODULE/templates/js/myModuleDep.source.js');
var config = {
map: {
'myModule': System.setPath('myModule'),
'myModuleDep': System.setPath('myModuleDep')
}
meta: {}
};
// as of System.js v0.20.* you'll definetely need to tell meta your intensions per-module
// note I couldn't get wildcarding to work, hence the individual setPath's
config.meta[System.setPath('myModule')] = {format: 'amd'};
config.meta[System.setPath('myModuleDep')] = {format: 'amd'};
System.config(config);
delete window.config;
// a getter/setter for your new tokenized paths
// enables centralized attachment of a cache-busting query parameter accross your app
System.setPath = function(token, path) {
if (! path) {
if (this.cbPaths[token]) {
return this.cbPaths[token] + (!! ~this.cbPaths[token].indexOf('?') ? '&' : '?') + 'cbv=' + this.cbv;
}
} else {
this.cbPaths[token] = path;
}
return token;
}
System.cbv = '';
System.cbPaths = {};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment