Skip to content

Instantly share code, notes, and snippets.

@mrsharpoblunto
Last active September 6, 2019 16:06
Show Gist options
  • Save mrsharpoblunto/938e73631414fec4fa3e35ecf4128897 to your computer and use it in GitHub Desktop.
Save mrsharpoblunto/938e73631414fec4fa3e35ecf4128897 to your computer and use it in GitHub Desktop.
<script type="text/javascript">
// the server will write out the paths of any API calls it plans to
// run server-side so the client knows to wait for the server, rather
// than doing its own XHR request for the data
window.__data = {
'/my/api/path': {
waiting: [],
}
};
window.__dataLoaded = function(path, data) {
const cacheEntry = window.__data[path];
if (cacheEntry) {
cacheEntry.data = data;
for (var i = 0;i < cacheEntry.waiting.length; ++i) {
cacheEntry.waiting[i].resolve(cacheEntry.data);
}
cacheEntry.waiting = [];
}
};
</script>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment