@@ -75,6 +75,21 @@ function parseConfig(data) {
75
75
return config ;
76
76
}
77
77
78
+ async function getConfig ( file , isPreloaded ) {
79
+ const response = await fetch ( file , { credentials : 'same-origin' } ) ;
80
+ if ( response . status !== 200 ) {
81
+ if ( isPreloaded ) return parseConfig ( '' ) ;
82
+ throw new Error ( `Failed to load config.yml (${ response . status } )` ) ;
83
+ }
84
+ const contentType = response . headers . get ( 'Content-Type' ) || 'Not-Found' ;
85
+ const isYaml = contentType . indexOf ( 'yaml' ) !== - 1 ;
86
+ if ( ! isYaml ) {
87
+ console . log ( `Response for ${ file } was not yaml. (Content-Type: ${ contentType } )` ) ;
88
+ if ( isPreloaded ) return parseConfig ( '' ) ;
89
+ }
90
+ return parseConfig ( await response . text ( ) ) ;
91
+ }
92
+
78
93
export function configLoaded ( config ) {
79
94
return {
80
95
type : CONFIG_SUCCESS ,
@@ -115,19 +130,12 @@ export function loadConfig() {
115
130
116
131
try {
117
132
const preloadedConfig = getState ( ) . config ;
118
- const response = await fetch ( 'config.yml' , { credentials : 'same-origin' } )
119
- const requestSuccess = response . status === 200 ;
120
-
121
- if ( ! preloadedConfig && ! requestSuccess ) {
122
- throw new Error ( `Failed to load config.yml (${ response . status } )` ) ;
123
- }
124
-
125
- const loadedConfig = parseConfig ( requestSuccess ? await response . text ( ) : '' ) ;
133
+ const loadedConfig = await getConfig ( 'config.yml' , preloadedConfig && preloadedConfig . size > 1 ) ;
126
134
127
135
/**
128
136
* Merge any existing configuration so the result can be validated.
129
137
*/
130
- const mergedConfig = mergePreloadedConfig ( preloadedConfig , loadedConfig )
138
+ const mergedConfig = mergePreloadedConfig ( preloadedConfig , loadedConfig ) ;
131
139
const config = flow ( validateConfig , applyDefaults ) ( mergedConfig ) ;
132
140
133
141
dispatch ( configDidLoad ( config ) ) ;
0 commit comments