Skip to content

Commit 83eb13a

Browse files
committed
Createa fallback for jsdelivr
1 parent 74823d1 commit 83eb13a

File tree

1 file changed

+15
-2
lines changed

1 file changed

+15
-2
lines changed

packages/app/src/sandbox/eval/npm/fetch-npm-module.js

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -73,10 +73,10 @@ const TEMP_USE_JSDELIVR = false;
7373
// Strips the version of a path, eg. test/1.3.0 -> test
7474
const ALIAS_REGEX = /\/\d*\.\d*\.\d*.*?(\/|$)/;
7575

76-
function getUnpkgUrl(name: string, version: string) {
76+
function getUnpkgUrl(name: string, version: string, forceJsDelivr?: boolean) {
7777
const nameWithoutAlias = name.replace(ALIAS_REGEX, '');
7878

79-
return TEMP_USE_JSDELIVR // TODO: change to TEMP_USE_JSDELIVR
79+
return TEMP_USE_JSDELIVR || forceJsDelivr
8080
? `https://cdn.jsdelivr.net/npm/${nameWithoutAlias}@${version}`
8181
: `https://unpkg.com/${nameWithoutAlias}@${version}`;
8282
}
@@ -125,6 +125,19 @@ function downloadDependency(depName: string, depVersion: string, path: string) {
125125
return x.text();
126126
}
127127

128+
if (isGitHub) {
129+
// Fall back to jsdelivr
130+
return fetch(
131+
`${getUnpkgUrl(depName, depVersion, true)}${relativePath}`
132+
).then(x2 => {
133+
if (x2.ok) {
134+
return x2.text();
135+
}
136+
137+
throw new Error(`Could not find module ${path}`);
138+
});
139+
}
140+
128141
throw new Error(`Could not find module ${path}`);
129142
})
130143
.then(x => ({

0 commit comments

Comments
 (0)