Skip to content

Commit

Permalink
Runtime environment variables in React
Browse files Browse the repository at this point in the history
  • Loading branch information
melissachang committed May 23, 2018
1 parent 81cd2ae commit 1212642
Show file tree
Hide file tree
Showing 5 changed files with 43 additions and 8 deletions.
12 changes: 12 additions & 0 deletions docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,18 @@ services:
- ./ui:/ui
# See https://stackoverflow.com/a/32785014 for why this is needed.
- /ui/node_modules
ui-prod:
build:
context: ui
dockerfile: Dockerfile.prod
ports:
- 127.0.0.1:80:80
volumes:
- ./ui:/ui
# See https://stackoverflow.com/a/32785014 for why this is needed.
- /ui/node_modules
environment:
- API_URL=bar
# Can't use "api" because it maps to something else on Google network.
# Use "apise" for "api server". ("apis" is also taken.)
apise:
Expand Down
17 changes: 17 additions & 0 deletions ui/Dockerfile.prod
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
FROM node as builder
WORKDIR /ui
COPY package.json package-lock.json /ui/
# src/api contains Swagger-generated data_explorer_service. This is needed by
# "npm install" beacuse package.json has a local dependency on
# data_explorer_service.
COPY src/api /ui/src/api
RUN npm install
RUN npm install react-scripts@1.1.1 -g
COPY . /ui
RUN npm run build

ENV API_URL foo

FROM nginx
COPY --from=builder /ui/build /usr/share/nginx/html
CMD sed -i "s/API_URL_REPLACE_ME/$API_URL/" /usr/share/nginx/html/env.js && nginx -g "daemon off;"
8 changes: 8 additions & 0 deletions ui/public/env.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
// Create React App doesn't have an easy way to set run-time environment
// variables. Here we copy
// https://github.com/facebook/create-react-app/issues/578#issuecomment-277843310

window.env = {
// This option can be retrieved in "src/index.js" with "window.env.API_URL".
API_URL: 'API_URL_REPLACE_ME'
};
12 changes: 4 additions & 8 deletions ui/public/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -27,14 +27,10 @@
</noscript>
<div id="root"></div>
<!--
This HTML file is a template.
If you open it directly in the browser, you will see an empty page.
You can add webfonts, meta tags, or analytics to this file.
The build step will place the bundled scripts into the <body> tag.
To begin the development, run `npm start` or `yarn start`.
To create a production bundle, use `npm run build` or `yarn build`.
Load "env.js" script before the main JavaScript bundle.
You can use it to define "runtime" environment variables.
Please open this file for more details.
-->
<script src="%PUBLIC_URL%/env.js"></script>
</body>
</html>
2 changes: 2 additions & 0 deletions ui/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,7 @@ import "./index.css";
import App from "./App";
import registerServiceWorker from "./registerServiceWorker";

console.log('API_URL is ' + window.env.API_URL);
ReactDOM.render(<App />, document.getElementById("root"));
registerServiceWorker();

0 comments on commit 1212642

Please sign in to comment.