Skip to content

Commit

Permalink
chore: use deferred scripts for faster demo startup (#1083)
Browse files Browse the repository at this point in the history
  • Loading branch information
gkatsev authored Feb 25, 2021
1 parent 512bba5 commit c348174
Showing 1 changed file with 23 additions and 16 deletions.
39 changes: 23 additions & 16 deletions scripts/index-demo-page.js
Original file line number Diff line number Diff line change
Expand Up @@ -166,21 +166,6 @@
// eslint-disable-next-line
var reloadScripts = function(urls, cb) {
var el = document.getElementById('reload-scripts');
var onload = function() {
var script;

if (!urls.length) {
cb();
return;
}

script = document.createElement('script');

script.src = urls.shift();
script.onload = onload;

el.appendChild(script);
};

if (!el) {
el = document.createElement('div');
Expand All @@ -192,7 +177,29 @@
el.removeChild(el.firstChild);
}

onload();
const loaded = [];

function checkDone() {
if (loaded.length === urls.length) {
cb();
}
}

urls.forEach((url) => {
const script = document.createElement('script');
// scripts marked as defer will be loaded asynchronously but will be executed in the order they are in the DOM
script.defer = true;
// dynamically created scripts are async by default unless otherwise specified
// async scripts are loaded asynchronously but also executed as soon as they are loaded
// we want to load them in the order they are added therefore we want to turn off async
script.async = false;
script.src = url;
script.onload = () => {
loaded.push(url);
checkDone();
};
el.appendChild(script);
});
};

var regenerateRepresentations = function() {
Expand Down

0 comments on commit c348174

Please sign in to comment.