[frontend] Fix issue with async loading of js resources in the dashboard
This switches the loading to use new Function(...)() instead of eval to prevent context issues and speed it up a bit more.
@@ -778,6 +778,16 @@ ${ smart_unicode(login_modal(request).content) | n,unicode }
loadedCss.push($(this).attr('href'));
});
+ var loadScript = function (scriptUrl) {
+ var deferred = $.Deferred();
+ $.get(scriptUrl).done(function (contents) {
+ loadedJs.push(scriptUrl);
+ deferred.resolve(contents);
+ }).fail(function () {
+ deferred.resolve('');
+ });
+ return deferred.promise();
+ };
var loadScripts = function (scriptUrls) {
var promises = [];
@@ -786,15 +796,7 @@ ${ smart_unicode(login_modal(request).content) | n,unicode }
if (loadedJs.indexOf(scriptUrl) !== -1) {
continue;
}
- var loadDeferred = $.Deferred();
- promises.push(loadDeferred.promise());
- loadDeferred.always(function () {
- loadedJs.push(scriptUrl);
- });
- $.get(scriptUrl).done(loadDeferred.resolve).fail(function () {
- // Ignore failed ones, they'll still appear as failed in the network tab as before
- loadDeferred.resolve('');
- });
+ promises.push(loadScript(scriptUrl));
}
return promises;
};
@@ -827,8 +829,6 @@ ${ smart_unicode(login_modal(request).content) | n,unicode }
}).toArray();
$allScripts.remove();
- var scriptPromises = loadScripts(scriptsToLoad);
-
$rawHtml.find('link[href]').each(function () {
addGlobalCss($(this)); // Also removes the elements;
});
@@ -850,13 +850,15 @@ ${ smart_unicode(login_modal(request).content) | n,unicode }
$rawHtml.unwrap('<span>');
+ var scriptPromises = loadScripts(scriptsToLoad);
+
var evalScriptSync = function () {
if (scriptPromises.length) {
// Evaluate the scripts in the order they were defined in the page
var nextScriptPromise = scriptPromises.shift();
nextScriptPromise.done(function (contents) {
if (contents) {
- eval(contents);
+ new Function(contents)();
}
evalScriptSync();
});
