Skip to content

Commit 68a5d84

Browse files
committed
Fallback for dynamic-importmap
1 parent 78db417 commit 68a5d84

File tree

1 file changed

+14
-11
lines changed

1 file changed

+14
-11
lines changed

src/vitessce/widget.py

Lines changed: 14 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -155,7 +155,14 @@ def get_uid_str(uid):
155155

156156
# lang: js
157157
ESM = """
158-
import { importWithMap } from 'https://unpkg.com/[email protected]';
158+
let importWithMap;
159+
try {
160+
importWithMap = (await import('https://unpkg.com/[email protected]')).importWithMap;
161+
} catch(e) {
162+
console.warn("Import of dynamic-importmap failed, trying fallback.");
163+
importWithMap = (await import('https://cdn.vitessce.io/[email protected]/dist/index.js')).importWithMap;
164+
}
165+
159166
const successfulImportMap = {
160167
imports: {
161168
@@ -170,11 +177,10 @@ def get_uid_str(uid):
170177
};
171178
const fallbackImportMap = {
172179
imports: {
173-
//"react": "https://cdn.vitessce.io/[email protected]/index.js",
174-
//"react-dom": "https://cdn.vitessce.io/[email protected]/index.js",
175-
//"react-dom/client": "https://cdn.vitessce.io/[email protected]/es2022/client.mjs",
176-
177-
// Replace with version-specific URL below.
180+
"react": "https://cdn.vitessce.io/[email protected]/index.js",
181+
"react-dom": "https://cdn.vitessce.io/[email protected]/index.js",
182+
"react-dom/client": "https://cdn.vitessce.io/[email protected]/es2022/client.mjs",
183+
// Replaced with version-specific URL below.
178184
"vitessce": "https://cdn.vitessce.io/[email protected]/dist/index.min.js",
179185
},
180186
};
@@ -184,15 +190,13 @@ def get_uid_str(uid):
184190
"react": "https://cdn.vitessce.io/[email protected]/index_dev.js",
185191
"react-dom": "https://cdn.vitessce.io/[email protected]/index_dev.js",
186192
"react-dom/client": "https://cdn.vitessce.io/[email protected]/es2022/client.development.mjs",
187-
// Replace with version-specific URL below.
193+
// Replaced with version-specific URL below.
188194
"vitessce": "https://cdn.vitessce.io/@vitessce/[email protected]/dist/index.js",
189195
},
190196
};
191197
*/
192198
193199
async function importWithMapAndFallback(moduleName, importMap, fallbackMap) {
194-
console.log(`Importing ${moduleName} with import map`, importMap, fallbackMap);
195-
196200
let result = null;
197201
if (!fallbackMap) {
198202
// fallbackMap is null, user may have provided custom JS URL.
@@ -213,7 +217,7 @@ def get_uid_str(uid):
213217
});
214218
successfulImportMap.imports[moduleName] = importMap.imports[moduleName];
215219
} catch (e) {
216-
console.warn(`Importing ${moduleName} failed with import map`, importMap, e);
220+
console.warn(`Importing ${moduleName} failed with importMap`, importMap, "trying fallback", fallbackMap, successfulImportMap);
217221
result = await importWithMap(moduleName, {
218222
imports: {
219223
...fallbackMap.imports,
@@ -315,7 +319,6 @@ def get_uid_str(uid):
315319
}
316320
317321
async function render(view) {
318-
console.log("Rendering Vitessce widget...");
319322
const cssUid = view.model.get('uid');
320323
const jsDevMode = view.model.get('js_dev_mode');
321324
const jsPackageVersion = view.model.get('js_package_version');

0 commit comments

Comments
 (0)