Skip to content

Commit 56576ec

Browse files
Gene Connollygconnolly
authored andcommitted
add readme
1 parent 156ee38 commit 56576ec

File tree

2 files changed

+166
-26
lines changed

2 files changed

+166
-26
lines changed

README.md

Lines changed: 165 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,44 +1,184 @@
1-
This project was bootstrapped with [Create React App](https://github.com/facebook/create-react-app).
1+
# ng-immutable-example
22

3-
## Available Scripts
3+
This project demonstrates how to convert to an [Immutable Web App](https://immutablwebapps.org) from an Angular app that was generated with [Create React App](https://github.com/facebook/create-react-app) version .
44

5-
In the project directory, you can run:
5+
## Getting Started
66

7-
### `npm start`
7+
This project was created by running:
88

9-
Runs the app in the development mode.<br>
10-
Open [http://localhost:3000](http://localhost:3000) to view it in the browser.
9+
```bash
10+
> npx create-react-app react-immutable-example
1111

12-
The page will reload if you make edits.<br>
13-
You will also see any lint errors in the console.
12+
> cd react-immutable-example
13+
```
1414

15-
### `npm test`
15+
## Immutable Conversion
1616

17-
Launches the test runner in the interactive watch mode.<br>
18-
See the section about [running tests](https://facebook.github.io/create-react-app/docs/running-tests) for more information.
17+
Converting this this new application to build an Immutable Web App requires the following steps:
1918

20-
### `npm run build`
19+
1. Referencing environment variables defined on `window`
20+
2. Rendering an `index.html` template for deployments
21+
3. Running locally
2122

22-
Builds the app for production to the `build` folder.<br>
23-
It correctly bundles React in production mode and optimizes the build for the best performance.
23+
## Referencing environment variables defined on `window`
2424

25-
The build is minified and the filenames include the hashes.<br>
26-
Your app is ready to be deployed!
25+
Create React App has documentation for [adding custome environment variables](https://facebook.github.io/create-react-app/docs/adding-custom-environment-variables). As they mention in the documentation, the environment variables are embedded during the build-time. To make the assets immutable, the setting of values must be shifted from build-time to run-time. Create React App has documentation for [injecting data from the server into the page](https://facebook.github.io/create-react-app/docs/title-and-meta-tags#injecting-data-from-the-server-into-the-page) which is similar to how this Immutable Web App example will handle environment variables.
2726

28-
See the section about [deployment](https://facebook.github.io/create-react-app/docs/deployment) for more information.
27+
Change all references to point to the run-time `window` object instead of the build-time `process` object:
2928

30-
### `npm run eject`
29+
```diff
30+
- process.env.REACT_APP_VARIABLE
31+
+ window.env.REACT_APP_VARIABLE
32+
```
3133

32-
**Note: this is a one-way operation. Once you `eject`, you can’t go back!**
34+
## Rendering an `index.html` template for deployments
3335

34-
If you aren’t satisfied with the build tool and configuration choices, you can `eject` at any time. This command will remove the single build dependency from your project.
36+
In theory, the role of `index.html` in an Immutable Web App is to be a deployment manifest that only contains configuration that is unique to the environment where it is deployed.
3537

36-
Instead, it will copy all the configuration files and the transitive dependencies (Webpack, Babel, ESLint, etc) right into your project so you have full control over them. All of the commands except `eject` will still work, but they will point to the copied scripts so you can tweak them. At this point you’re on your own.
38+
In practice, there is often a need to include markup or scripts in the `index.html` that are more than just configuration, or that doesn't vary by environment, or that does change between versions of the app. This issue can be resolved by creating an immutable `index.html` template, that is published with the other immutable assets.
3739

38-
You don’t have to ever use `eject`. The curated feature set is suitable for small and middle deployments, and you shouldn’t feel obligated to use this feature. However we understand that this tool wouldn’t be useful if you couldn’t customize it when you are ready for it.
40+
### Converting index.html to a template
3941

40-
## Learn More
42+
This example uses [EJS](https://ejs.co/) as the templating language to render `index.html`, but any templating language can be used.
4143

42-
You can learn more in the [Create React App documentation](https://facebook.github.io/create-react-app/docs/getting-started).
44+
1) Identify the parts of the `index.html` that vary by environment:
4345

44-
To learn React, check out the [React documentation](https://reactjs.org/).
46+
- [`PUBLIC_URL`](https://facebook.github.io/create-react-app/docs/using-the-public-folder#adding-assets-outside-of-the-module-system): URL where the versioned immutable assets will be deployed.
47+
- `window.env`: Environment-specific javascript configuration.
48+
49+
2) Set `PUBLIC_URL` to an EJS template string in the `.env`:
50+
51+
```diff
52+
+ PUBLIC_URL=<%=PUBLIC_URL%>
53+
```
54+
55+
3) Add a new script tag to render the javascript environment-specific variables in `public/index.html`:
56+
57+
```diff
58+
<head>
59+
...
60+
+ <script>
61+
+ env = %REACT_APP_CLIENT_ENV%;
62+
+ </script>
63+
</head>
64+
```
65+
66+
4) Set `REACT_APP_CLIENT_ENV` to an EJS template string in the `.env`:
67+
68+
```diff
69+
PUBLIC_URL=<%=PUBLIC_URL%>
70+
+ REACT_APP_CLIENT_ENV=<%-JSON.stringify(env)%>
71+
```
72+
73+
### Example
74+
75+
#### `public/index.html`:
76+
77+
__The source template that is converted into an immutable template during the build.__
78+
79+
```html
80+
<!DOCTYPE html>
81+
<html lang="en">
82+
<head>
83+
<meta charset="utf-8">
84+
<link rel="shortcut icon" href="%PUBLIC_URL%/favicon.ico">
85+
<meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
86+
<meta name="theme-color" content="#000000">
87+
<link rel="manifest" href="%PUBLIC_URL%/manifest.json">
88+
<title>React App</title>
89+
<script>
90+
env = %REACT_APP_ENV%;
91+
</script>
92+
</head>
93+
<body>
94+
<noscript>
95+
You need to enable JavaScript to run this app.
96+
</noscript>
97+
<div id="root"></div>
98+
</body>
99+
</html>
100+
```
101+
102+
#### `.env`
103+
104+
__The build-time environment variables used to generate the immutable template.__
105+
106+
```sh
107+
PUBLIC_URL=<%=PUBLIC_URL%>
108+
REACT_APP_ENV=<%-JSON.stringify(env)%>
109+
```
110+
111+
#### `dist/ng-immutable-example/index.ejs`
112+
113+
__The immutable template that is published along with the other versioned, immutable assets.__ It is combined with `production-config.json` to render the `index.html` that is a deployment manifest.
114+
115+
```html
116+
<!doctype html>
117+
<html lang="en">
118+
<head>
119+
<meta charset="utf-8">
120+
<link rel="shortcut icon" href="<%=PUBLIC_URL%>/favicon.ico">
121+
<meta name="viewport" content="width=device-width,initial-scale=1,shrink-to-fit=no">
122+
<meta name="theme-color" content="#000000">
123+
<link rel="manifest" href="<%=PUBLIC_URL%>/manifest.json">
124+
<title>React App</title>
125+
<script>env = <%-JSON.stringify(env)%>;</script>
126+
<link href="<%=PUBLIC_URL%>/static/css/main.6bd13355.chunk.css" rel="stylesheet">
127+
</head>
128+
<body>
129+
<noscript>
130+
You need to enable JavaScript to run this app.
131+
</noscript>
132+
<div id="root"></div>
133+
<script>...</script>
134+
<script src="<%=PUBLIC_URL%>/static/js/1.a700ff87.chunk.js"></script>
135+
<script src="<%=PUBLIC_URL%>/static/js/main.b75225a8.chunk.js"></script>
136+
</body>
137+
</html>
138+
```
139+
140+
#### `config.json`:
141+
142+
__The environments-specific values that vary.__ This is combined with the published immutable template to be rendered into `index.html`. It is not an immutable asset and should be managed as deployment-specific configuration.
143+
144+
```json
145+
{
146+
"PUBLIC_URL": "https://assets.react-immutable-example.com/1.0.0",
147+
"env": {
148+
"api": "https://api.react-immutable-example.com"
149+
}
150+
}
151+
```
152+
153+
#### Rendered `index.html`
154+
155+
__The environment-specific deployment manifest.__ It is the product of rendering an immutable template against the `config.json`. Publishing this file to the web application environment is an atomic deployment.
156+
157+
```html
158+
<!doctype html>
159+
<html lang="en">
160+
<head>
161+
<meta charset="utf-8">
162+
<link rel="shortcut icon" href="https://assets.react-immutable-example.com/1.0.0/favicon.ico">
163+
<meta name="viewport" content="width=device-width,initial-scale=1,shrink-to-fit=no">
164+
<meta name="theme-color" content="#000000">
165+
<link rel="manifest" href="https://assets.react-immutable-example.com/1.0.0/manifest.json">
166+
<title>React App</title>
167+
<script>env = { "api": "https://api.react-immutable-example.com" };</script>
168+
<link href="https://assets.react-immutable-example.com/1.0.0/static/css/main.6bd13355.chunk.css" rel="stylesheet">
169+
</head>
170+
<body>
171+
<noscript>
172+
You need to enable JavaScript to run this app.
173+
</noscript>
174+
<div id="root"></div>
175+
<script>...</script>
176+
<script src="https://assets.react-immutable-example.com/1.0.0/static/js/1.a700ff87.chunk.js"></script>
177+
<script src="https://assets.react-immutable-example.com/1.0.0/static/js/main.b75225a8.chunk.js"></script>
178+
</body>
179+
</html>
180+
```
181+
182+
## Future Work
183+
184+
This project will be updated as better patterns for building Immutable Web Apps are established and as Angular CLI changes.

public/index.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
-->
2222
<title>React App</title>
2323
<script>
24-
env = %REACT_APP_CLIENT_ENV%;
24+
env = %REACT_APP_ENV%;
2525
</script>
2626
</head>
2727
<body>

0 commit comments

Comments
 (0)