diff --git a/src/config-loader.js b/src/config-loader.js index cbacdf1..e66530b 100644 --- a/src/config-loader.js +++ b/src/config-loader.js @@ -23,7 +23,9 @@ const AUTH_TOKEN_KEY = 'peransa_token'; const VERSION_RETRY_ROUNDS = 3; const CONFIG_RETRY_ROUNDS = 3; - const CONFIG_TIMEOUT = 12000; + const VERSION_FETCH_TIMEOUT_MS = 20000; + const CONFIG_FETCH_TIMEOUT_MS = 25000; + const CONFIG_TIMEOUT = 60000; let configTimeoutId; let mainScriptLoaded = false; @@ -381,13 +383,22 @@ } const contentType = response.headers.get('content-type') || ''; + const text = await response.text(); + const trimmed = text.trim(); - if (!contentType.includes('application/json')) { - const text = await response.text(); - throw new Error('Response is not JSON. Received: ' + text.substring(0, 100)); + if (contentType.includes('application/json') || trimmed.startsWith('{') || trimmed.startsWith('[')) { + try { + return JSON.parse(trimmed); + } catch (error) { + throw new Error('Response is not JSON. Received: ' + trimmed.substring(0, 100)); + } } - return await response.json(); + if (trimmed) { + return trimmed; + } + + throw new Error('Response is empty'); } finally { window.clearTimeout(timeoutId); } @@ -400,10 +411,9 @@ for (let round = 1; round <= rounds; round++) { for (let index = 0; index < urls.length; index++) { const url = urls[index]; - const timeout = index === urls.length - 1 ? 6000 : 3500; try { - const data = await fetchJson(url, timeout); + const data = await fetchJson(url, VERSION_FETCH_TIMEOUT_MS); const version = normalizeVersionResponse(data); if (!version) { throw new Error('Version response is empty'); @@ -429,10 +439,9 @@ for (let round = 1; round <= rounds; round++) { for (let index = 0; index < urls.length; index++) { const url = urls[index]; - const timeout = index === urls.length - 1 ? 7000 : 4000; try { - const config = await fetchJson(url, timeout); + const config = await fetchJson(url, CONFIG_FETCH_TIMEOUT_MS); if (!config || typeof config !== 'object') { throw new Error('Config response is invalid');