Skip to content

Commit a91bb80

Browse files
committed
Probe runtime config before passing control to app
In order to use released XSnippet Web in various environments, we need to support so called runtime configuration. Basically, it's nothing more but an attempt to download some JSON and merge its values with default settings. Runtime configuration is quite handy and could be useful, for example, when you want to use another (non default) API server.
1 parent 47b7158 commit a91bb80

File tree

2 files changed

+39
-10
lines changed

2 files changed

+39
-10
lines changed

src/components/App.jsx

Lines changed: 34 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3,17 +3,42 @@ import React from 'react'
33
import Header from './Header'
44
import Main from './Main'
55
import Sidebar from './Sidebar'
6+
import Spinner from './common/Spinner'
7+
import conf from '../conf'
68

79
import '../styles/App.styl'
810

9-
const App = () => (
10-
[
11-
<Header key="header" />,
12-
<div className="content" key="content">
13-
<Sidebar />
14-
<Main />
15-
</div>,
16-
]
17-
)
11+
class App extends React.Component {
12+
constructor(props) {
13+
super(props)
14+
this.state = {
15+
isLoading: true,
16+
}
17+
18+
fetch(process.env.RUNTIME_CONF_URI)
19+
.then((response) => {
20+
if (response.status === 404) {
21+
return {}
22+
}
23+
return response.json()
24+
})
25+
.then(json => Object.assign(conf, json))
26+
.then(() => this.setState({ isLoading: false }))
27+
}
28+
29+
render() {
30+
if (this.state.isLoading) {
31+
return <Spinner />
32+
}
33+
34+
return [
35+
<Header key="header" />,
36+
<div className="content" key="content">
37+
<Sidebar />
38+
<Main />
39+
</div>,
40+
]
41+
}
42+
}
1843

1944
export default App

webpack.config.js

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,9 @@ module.exports = () => {
109109
'xml',
110110
'yaml',
111111
'django']
112+
const assetsPath = process.env.ASSETS_PATH == null
113+
? '/'
114+
: process.env.ASSETS_PATH
112115

113116
const conf = {
114117
// Assume we are targeting production environments by default; the value
@@ -145,7 +148,7 @@ module.exports = () => {
145148
output: {
146149
filename: '[name].[chunkhash].js',
147150
path: path.resolve(__dirname, 'dist'),
148-
publicPath: process.env.ASSET_PATH || '/',
151+
publicPath: assetsPath,
149152
},
150153

151154
module: {
@@ -219,6 +222,7 @@ module.exports = () => {
219222
new webpack.EnvironmentPlugin({
220223
API_BASE_URI: null,
221224
RAW_SNIPPET_URI_FORMAT: null,
225+
RUNTIME_CONF_URI: `${assetsPath}conf.json`,
222226
}),
223227

224228
// Similar to JavaScript, we use [chunkhash] in order to invalidate

0 commit comments

Comments
 (0)