forked from codesandbox/codesandbox-client
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexternal-resources.ts
More file actions
79 lines (66 loc) · 1.92 KB
/
external-resources.ts
File metadata and controls
79 lines (66 loc) · 1.92 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
function getExternalResourcesConcatenation(resources: Array<string>) {
return resources.join('');
}
/* eslint-disable no-cond-assign */
function clearExternalResources() {
let el = null;
// eslint-disable-next-line no-cond-assign
while ((el = document.getElementById('external-css'))) {
el.remove();
}
// eslint-disable-next-line no-cond-assign
while ((el = document.getElementById('external-js'))) {
el.remove();
}
}
/* eslint-enable */
function addCSS(resource: string) {
const head = document.getElementsByTagName('head')[0];
const link = document.createElement('link');
link.id = 'external-css';
link.rel = 'stylesheet';
link.type = 'text/css';
link.href = resource;
link.media = 'all';
head.appendChild(link);
return link;
}
function addJS(resource: string) {
const script = document.createElement('script');
script.setAttribute('src', resource);
script.async = false;
script.setAttribute('id', 'external-js');
document.head.appendChild(script);
return script;
}
function addResource(resource: string) {
const match = resource.match(/\.([^.]*)$/);
const el =
(match && match[1] === 'css') || resource.includes('fonts.googleapis')
? addCSS(resource)
: addJS(resource);
return new Promise(r => {
el.onload = r;
el.onerror = r;
});
}
function waitForLoaded() {
return new Promise(resolve => {
if (document.readyState !== 'complete') {
window.addEventListener('load', resolve);
} else {
resolve();
}
});
}
let cachedExternalResources = '';
export default async function handleExternalResources(externalResources) {
const extResString = getExternalResourcesConcatenation(externalResources);
if (extResString !== cachedExternalResources) {
clearExternalResources();
await Promise.all(externalResources.map(addResource));
cachedExternalResources = extResString;
return waitForLoaded();
}
return Promise.resolve();
}