Skip to content

Commit 3ed6518

Browse files
committed
update
1 parent eed4b98 commit 3ed6518

1 file changed

Lines changed: 24 additions & 3 deletions

File tree

app/src/cards.js

Lines changed: 24 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,16 @@
11
let cardsCache = null;
22

3+
const cardsImportPath = '../data/cards.json';
4+
5+
const cardsRequestUrl = (() => {
6+
try {
7+
return new URL(cardsImportPath, import.meta.url).href;
8+
} catch (error) {
9+
console.warn('Unable to resolve cards.json via import.meta.url, falling back to relative path', error);
10+
return 'data/cards.json';
11+
}
12+
})();
13+
314
const cardPalette = [
415
'#fde68a',
516
'#bfdbfe',
@@ -103,21 +114,31 @@ function shuffleCards(cards) {
103114

104115
async function loadFromImport() {
105116
try {
106-
const module = await import('../data/cards.json');
117+
const module = await import(cardsImportPath, { assert: { type: 'json' } });
107118
const payload = module.default ?? module;
108119
if (!Array.isArray(payload)) {
109120
return null;
110121
}
111122

112123
return payload;
113124
} catch (error) {
114-
console.warn('Falling back to network fetch for cards', error);
125+
try {
126+
const module = await import(cardsImportPath);
127+
const payload = module.default ?? module;
128+
if (!Array.isArray(payload)) {
129+
return null;
130+
}
131+
132+
return payload;
133+
} catch (nestedError) {
134+
console.warn('Falling back to network fetch for cards', nestedError);
135+
}
115136
return null;
116137
}
117138
}
118139

119140
async function loadFromNetwork() {
120-
const response = await fetch('data/cards.json', { cache: 'no-store' });
141+
const response = await fetch(cardsRequestUrl, { cache: 'no-store' });
121142

122143
if (!response.ok) {
123144
throw new Error(`Cards request failed with status ${response.status}`);

0 commit comments

Comments
 (0)