Skip to content

Commit 7507cea

Browse files
committed
fix: verbessere die Logik zum Laden von Vokabularien mit CORS-Proxy-Fallback und erweitere die Konfiguration für Vokabular-URLs
1 parent 07c5070 commit 7507cea

File tree

2 files changed

+48
-9
lines changed

2 files changed

+48
-9
lines changed

src/lib/stores/settingsStore.svelte.ts

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -481,20 +481,25 @@ export class SettingsStore {
481481
}
482482

483483
// AMB Vocabulary Configuration
484+
// Unterstützt beide Key-Namen: "vocabularies" (config.json) und "vocabularyUrls" (config.live.json)
484485
if (config.amb) {
485486
const ambPartial: Partial<SettingsState> = {};
486487

487-
if (config.amb.vocabularies) {
488+
const vocabConfig = config.amb.vocabularies || config.amb.vocabularyUrls;
489+
if (vocabConfig) {
488490
ambPartial.vocabularyUrls = {
489-
audience: config.amb.vocabularies.audience ?? null,
490-
educationalLevel: config.amb.vocabularies.educationalLevel ?? null,
491-
learningResourceType: config.amb.vocabularies.learningResourceType ?? null,
492-
about: config.amb.vocabularies.about ?? null,
491+
audience: vocabConfig.audience ?? null,
492+
educationalLevel: vocabConfig.educationalLevel ?? null,
493+
learningResourceType: vocabConfig.learningResourceType ?? null,
494+
about: vocabConfig.about ?? null,
493495
};
494496
}
495497

498+
// Unterstützt "cacheTTL" (ms) und "cacheTtlHours" (Stunden → ms Konvertierung)
496499
if (config.amb.cacheTTL !== undefined) {
497500
ambPartial.vocabularyCacheTTL = config.amb.cacheTTL;
501+
} else if (config.amb.cacheTtlHours !== undefined) {
502+
ambPartial.vocabularyCacheTTL = config.amb.cacheTtlHours * 60 * 60 * 1000;
498503
}
499504

500505
if (Object.keys(ambPartial).length > 0) {

src/lib/utils/vocabularyLoader.ts

Lines changed: 38 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -339,13 +339,14 @@ export async function loadVocabulary(
339339
}
340340
}
341341

342-
// 3. Von URL laden (mit CORS-Proxy für skohub.io)
343-
// skohub.io erlaubt keine direkten Browser-Anfragen
344-
const fetchUrl = url.includes('skohub.io')
342+
// 3. Von URL laden (mit CORS-Proxy Fallback)
343+
// Versuche erst direkt, dann mit CORS-Proxy bei Netzwerk-/CORS-Fehler
344+
const needsProxy = url.includes('skohub.io');
345+
const fetchUrl = needsProxy
345346
? CORS_PROXY_URL + encodeURIComponent(url)
346347
: url;
347348

348-
console.log(`[VocabLoader] Fetching ${type} from ${url}`);
349+
console.log(`[VocabLoader] Fetching ${type} from ${url}${needsProxy ? ' (via CORS proxy)' : ''}`);
349350

350351
try {
351352
const response = await fetch(fetchUrl, {
@@ -381,6 +382,39 @@ export async function loadVocabulary(
381382
return concepts;
382383

383384
} catch (error) {
385+
// Falls kein Proxy benutzt wurde, mit CORS-Proxy erneut versuchen
386+
if (!needsProxy) {
387+
console.info(`[VocabLoader] Direct fetch failed for ${type}, retrying with CORS proxy...`);
388+
try {
389+
const proxyUrl = CORS_PROXY_URL + encodeURIComponent(url);
390+
const response = await fetch(proxyUrl, {
391+
headers: {
392+
'Accept': 'application/json, application/ld+json'
393+
}
394+
});
395+
396+
if (response.ok) {
397+
const data = await response.json();
398+
const concepts = parseSkosJsonLd(data, url);
399+
400+
if (concepts.length > 0) {
401+
const entry: VocabularyCacheEntry = {
402+
concepts,
403+
fetchedAt: Date.now(),
404+
url
405+
};
406+
memoryCache.set(type, entry);
407+
saveToLocalStorage(type, entry);
408+
console.log(`[VocabLoader] ✅ Loaded ${concepts.length} concepts for ${type} (via CORS proxy)`);
409+
return concepts;
410+
}
411+
}
412+
} catch (proxyError) {
413+
// Proxy hat auch nicht funktioniert → Fallback
414+
console.info(`[VocabLoader] CORS proxy also failed for ${type}`);
415+
}
416+
}
417+
384418
// Nur als Info loggen, da Fallback funktioniert
385419
console.info(`[VocabLoader] Network unavailable for ${type}, using built-in vocabulary`);
386420
return FALLBACK_VOCABULARIES[type];

0 commit comments

Comments
 (0)