Example of using app-server with a trivial create-react-app app.
git clone https://github.com/staticdeploy/app-server.git
cd app-server/examples/create-react-app
yarn install
# Start the development server
yarn start
Open the app at http://localhost:3000 and see the Hello world!
greeting.
Change the value of the variable defined in public/index.html
and see that the
greeting target has changed.
yarn start
simply starts create-react-app's development server.
When the app is loaded:
- the
#app-config
script in the app'sindex.html
defines the variablewindow.APP_CONFIG
- the appropriate greeting is rendered
git clone https://github.com/staticdeploy/app-server.git
cd app-server/examples/create-react-app
# Build the app Docker image
docker build -t app-server-example .
# Run the image passing in the necessary configuration
docker run --rm --init -p 3000:80 -e APP_CONFIG_TARGET=world app-server-example
Open the app at http://localhost:3000 and see the Hello world!
greeting. Stop
the container and restart it passing in a different value for the
APP_CONFIG_TARGET
variable. Re-open the app and see that the greeting target
has changed.
The docker build ...
command builds the app docker image, using the
staticdeploy/app-server:cra-builder
and staticdeploy/app-server:cra-runtime
base images which respectively:
- build the app into a static bundle
- setupapp-server to serve the bundle
When the image is run with docker run ...
, app-server
:
- starts serving the app using StaticDeploy's serving algorithm, that includes
features such as:
- using a fallback asset for requests to non-existing assets
- allowing the user to use non-canonical paths to request assets (e.g.
/foo/index.js
instead of/index.js
) - injecting the app configuration in the served html files
- specifying custom headers for assets
When the app is loaded:
- the
#app-config
script in the app'sindex.html
is evaluated - the content of the script defines the variable
window.APP_CONFIG
- the appropriate greeting is rendered