From d5cd79f2c4a1be3aff42a914cc2e52f937c289ef Mon Sep 17 00:00:00 2001 From: Tony Alves Date: Wed, 28 Mar 2018 13:25:46 -0700 Subject: [PATCH] Allow for un-captured route of config.yml (#1182) --- src/actions/config.js | 26 +++++++++++++++++--------- 1 file changed, 17 insertions(+), 9 deletions(-) diff --git a/src/actions/config.js b/src/actions/config.js index 12144a8d..6f272bba 100644 --- a/src/actions/config.js +++ b/src/actions/config.js @@ -75,6 +75,21 @@ function parseConfig(data) { return config; } +async function getConfig(file, isPreloaded) { + const response = await fetch(file, { credentials: 'same-origin' }); + if (response.status !== 200) { + if (isPreloaded) return parseConfig(''); + throw new Error(`Failed to load config.yml (${ response.status })`); + } + const contentType = response.headers.get('Content-Type') || 'Not-Found'; + const isYaml = contentType.indexOf('yaml') !== -1; + if (!isYaml) { + console.log(`Response for ${ file } was not yaml. (Content-Type: ${ contentType })`); + if (isPreloaded) return parseConfig(''); + } + return parseConfig(await response.text()); +} + export function configLoaded(config) { return { type: CONFIG_SUCCESS, @@ -115,19 +130,12 @@ export function loadConfig() { try { const preloadedConfig = getState().config; - const response = await fetch('config.yml', { credentials: 'same-origin' }) - const requestSuccess = response.status === 200; - - if (!preloadedConfig && !requestSuccess) { - throw new Error(`Failed to load config.yml (${ response.status })`); - } - - const loadedConfig = parseConfig(requestSuccess ? await response.text() : ''); + const loadedConfig = await getConfig('config.yml', preloadedConfig && preloadedConfig.size > 1); /** * Merge any existing configuration so the result can be validated. */ - const mergedConfig = mergePreloadedConfig(preloadedConfig, loadedConfig) + const mergedConfig = mergePreloadedConfig(preloadedConfig, loadedConfig); const config = flow(validateConfig, applyDefaults)(mergedConfig); dispatch(configDidLoad(config));