|
1 | 1 | let cardsCache = null; |
2 | 2 |
|
| 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 | + |
3 | 14 | const cardPalette = [ |
4 | 15 | '#fde68a', |
5 | 16 | '#bfdbfe', |
@@ -103,21 +114,31 @@ function shuffleCards(cards) { |
103 | 114 |
|
104 | 115 | async function loadFromImport() { |
105 | 116 | try { |
106 | | - const module = await import('../data/cards.json'); |
| 117 | + const module = await import(cardsImportPath, { assert: { type: 'json' } }); |
107 | 118 | const payload = module.default ?? module; |
108 | 119 | if (!Array.isArray(payload)) { |
109 | 120 | return null; |
110 | 121 | } |
111 | 122 |
|
112 | 123 | return payload; |
113 | 124 | } 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 | + } |
115 | 136 | return null; |
116 | 137 | } |
117 | 138 | } |
118 | 139 |
|
119 | 140 | async function loadFromNetwork() { |
120 | | - const response = await fetch('data/cards.json', { cache: 'no-store' }); |
| 141 | + const response = await fetch(cardsRequestUrl, { cache: 'no-store' }); |
121 | 142 |
|
122 | 143 | if (!response.ok) { |
123 | 144 | throw new Error(`Cards request failed with status ${response.status}`); |
|
0 commit comments