Skip to content

A platform for community discussion. Free, open, simple.

FIX: multiple loadScript to the same url may resolve prematurely (#6474)

Kyle Zhaoffc241eb253f
diff --git a/app/assets/javascripts/discourse/lib/load-script.js.es6 b/app/assets/javascripts/discourse/lib/load-script.js.es6
index 087ec344bc2..34b3f2897b7 100644
--- a/app/assets/javascripts/discourse/lib/load-script.js.es6
+++ b/app/assets/javascripts/discourse/lib/load-script.js.es6
@@ -11,7 +11,6 @@ function loadWithTag(path, cb) {
if (Ember.Test) {
Ember.Test.registerWaiter(() => finished);
}
- head.appendChild(s);
s.onload = s.onreadystatechange = function(_, abort) {
finished = true;
@@ -27,6 +26,8 @@ function loadWithTag(path, cb) {
}
}
};
+
+ head.appendChild(s);
}
export function loadCSS(url) {
@@ -41,17 +42,19 @@ export default function loadScript(url, opts) {
opts = opts || {};
+ // Scripts should always load from CDN
+ // CSS is type text, to accept it from a CDN we would need to handle CORS
+ url = opts.css ? Discourse.getURL(url) : Discourse.getURLWithCDN(url);
+
$("script").each((i, tag) => {
const src = tag.getAttribute("src");
- if (src && src !== url) {
- _loaded[tag.getAttribute("src")] = true;
+ if (src && src !== url && !_loading[src]) {
+ _loaded[src] = true;
}
});
return new Ember.RSVP.Promise(function(resolve) {
- url = Discourse.getURL(url);
-
// If we already loaded this url
if (_loaded[url]) {
return resolve();
@@ -78,23 +81,15 @@ export default function loadScript(url, opts) {
_loaded[url] = true;
};
- let cdnUrl = url;
-
- // Scripts should always load from CDN
- // CSS is type text, to accept it from a CDN we would need to handle CORS
- if (!opts.css && Discourse.CDN && url[0] === "/" && url[1] !== "/") {
- cdnUrl = Discourse.CDN.replace(/\/$/, "") + url;
- }
-
if (opts.css) {
ajax({
- url: cdnUrl,
+ url: url,
dataType: "text",
cache: true
}).then(cb);
} else {
// Always load JavaScript with script tag to avoid Content Security Policy inline violations
- loadWithTag(cdnUrl, cb);
+ loadWithTag(url, cb);
}
});
}