Skip to content

Commit eecb609

Browse files
committed
[docs] update/improve documentation to include dev-config-server
1 parent a97acee commit eecb609

6 files changed

+104
-77
lines changed

README.md

+6-26
Original file line numberDiff line numberDiff line change
@@ -17,35 +17,15 @@ configure any static app.
1717
### Install
1818

1919
```
20-
npm i --save-dev @staticdeploy/app-server
20+
yarn add -D @staticdeploy/app-server
2121
```
2222

23-
### Quickstart
23+
### Guides
2424

25-
- add the following `<script>` to your `index.html`:
26-
```html
27-
<script id="app-config">
28-
window.APP_CONFIG={
29-
MY_VAR: "default value for development"
30-
};
31-
</script>
32-
```
33-
When serving the file, `app-server` will inject the runtime configuration into
34-
the element.
25+
- [usage with create-react-app](docs/usage-with-cra.md)
26+
- [deploy create-react-app apps with Docker](docs/deploy-cra-apps-with-docker.md)
3527

36-
- access the config variables in your code:
37-
```js
38-
console.log(window.APP_CONFIG.MY_VAR);
39-
```
40-
41-
- start the server setting configuration variables:
42-
```sh
43-
env APP_CONFIG_MY_VAR=value app-server
44-
```
45-
46-
### Documentation
28+
### Additional documentation
4729

4830
- [how `window.APP_CONFIG` is generated](docs/config-generation.md)
49-
- [how the config script is injected into index.hmtl](docs/config-injection.md)
50-
- [how to dynamically set `window.APP_CONFIG` in development](docs/dynamic-config-in-dev.md)
51-
- [server configuration options](docs/server-configuration-options.md)
31+
- [app-server configuration options](docs/app-server-configuration-options.md)

docs/server-configuration-options.md renamed to docs/app-server-configuration-options.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
## Server configuration options
1+
## `app-server` configuration options
22

33
The `app-server` binary takes the following configuration options:
44

docs/config-injection.md

-12
This file was deleted.

docs/deploy-cra-apps-with-docker.md

+29
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
## Deploy create-react-app apps with Docker
2+
3+
Assuming you've set up your project as described in the
4+
[usage with create-react-app guide](usage-with-cra.md), then all you need to
5+
build a docker image for building and serving your app is using the following
6+
`Dockerfile`:
7+
8+
```Dockerfile
9+
FROM staticdeploy/app-server:cra-builder
10+
FROM staticdeploy/app-server:cra-runtime
11+
```
12+
13+
The first `FROM` instruction will run the `ONBUILD` instructions of the
14+
`:cra-builder` image, which will install dependencies with `yarn` and build the
15+
app with `yarn build`.
16+
17+
The second `FROM` instruction will copy the built app into the (relatively)
18+
small `:cra-runtime` image where `app-server` is installed and configured to
19+
serve the app.
20+
21+
You can then run your app image passing in configuration via environment
22+
variables:
23+
24+
```sh
25+
docker run -e APP_CONFIG_MY_VAR=my_val my-app-image
26+
```
27+
28+
> Note: the `Dockerfile`-s for the `staticdeploy/app-server:*` images can be
29+
> found in the [docker-app-server repository](https://github.com/staticdeploy/docker-app-server)

docs/dynamic-config-in-dev.md

-38
This file was deleted.

docs/usage-with-cra.md

+68
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
## Usage with create-react-app
2+
3+
### How-to
4+
5+
First:
6+
7+
- add the following `<script>` to your `public/index.html`:
8+
```html
9+
<script id="app-config" src="http://localhost:3456/app-config.js"></script>
10+
```
11+
12+
- modify the `start` npm script:
13+
```json
14+
"start": "dev-config-server & react-scripts start"
15+
```
16+
> Note: you can use `npm-run-all` or `concurrently` to better handle
17+
> concurrently running processes in npm scripts
18+
19+
- access the config variable in your code:
20+
```js
21+
console.log(window.APP_CONFIG.MY_VAR);
22+
```
23+
24+
Then:
25+
26+
#### In development
27+
28+
- define configuration in the `.env` file:
29+
```sh
30+
APP_CONFIG_MY_VAR=my_val
31+
```
32+
33+
- start the development server with `yarn start`
34+
35+
#### In production
36+
37+
- build your app with `yarn build`
38+
39+
- run `app-server` defining configuration via environment variables:
40+
```sh
41+
env APP_CONFIG_MY_VAR=my_val app-server
42+
```
43+
44+
### How it works
45+
46+
#### In development
47+
48+
`dev-config-server` starts a server listening on port `3456`. Reading
49+
environment variables defined in the `.env` file, it generates a javascript file
50+
and serves it at `/app-config.js`. The file defines the `window.APP_CONFIG`
51+
global variable ([how it's generated](config-generation.md)).
52+
53+
`react-scripts` starts the development server of the app.
54+
55+
When the app is loaded in the browser, the `#app-config` script in `index.html`
56+
loads `/app-config.js` defining `window.APP_CONFIG`. The variable can then be
57+
accessed by the app code.
58+
59+
#### In production
60+
61+
`app-server` starts a server listening on port `3000`, serving files under the
62+
`build` directory. It also generates - from environment variables - the
63+
javascript file defining `window.APP_CONFIG`. Instead of serving it at
64+
`/app-config.js` though, the server injects it directly as content of the
65+
`#app-config` script in `index.html` (removing the script's `src` attribute).
66+
67+
When the app is loaded in the browser, the `#app-config` script is evaluated,
68+
defining `window.APP_CONFIG` that can then be accessed by the app code.

0 commit comments

Comments
 (0)