Last active
March 27, 2017 23:34
-
-
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…
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
require([System.setPath('myModule')], function(mM) { | |
... | |
}; | |
define([System.setPath('myModuleDep')], function(mM) { | |
... | |
}; | |
import mMDep from System.setPath('myModuleDep'); |
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
// 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; |
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
// 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