Skip to content

Commit bae5732

Browse files
committed
rework the static assets example
1 parent 4ed7b64 commit bae5732

File tree

1 file changed

+16
-18
lines changed

1 file changed

+16
-18
lines changed

README.md

Lines changed: 16 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -31,32 +31,29 @@ Static assets are the files (javascript, css, images) that are generated from a
3131

3232
All of the leading application frameworks ([Angular CLI](https://github.com/angular/angular-cli/wiki/stories-application-environments), [Create React App](https://cli.vuejs.org/guide/mode-and-env.html#using-env-variables-in-client-side-code), [Ember CLI](https://ember-cli.com/user-guide/#Environments), [Vue CLI 3](https://cli.vuejs.org/guide/mode-and-env.html#using-env-variables-in-client-side-code)) recommend defining environment _values_ at _compile time_. This practice requires that the static assets are generated for each environment and regenerated for any change to an environment.
3333

34-
_Immutable Web Applications_ reference environment _variables_ that are defined on the global scope and that are set to values in the `index.html` that are unique to the deployment:
34+
_Immutable Web Applications_ reference environment _variables_ that are defined on the global scope and either referenced directly on the `window` object or through an injected service that is a wrapper for the environment variables:
3535

3636
```diff
37-
import { Injectable } from '@angular/core';
38-
import { HttpClient } from '@angular/common/http';
39-
40-
- // remove environment constants that are defined in the codebase or at compile time
41-
- import { environment } from './environments/environment';
42-
43-
@Injectable()
44-
export class DataService {
45-
configUrl = ;
37+
export class UserService {
38+
webServiceUrl: String;
4639

4740
constructor(
4841
private http: HttpClient
49-
private
50-
) { }
42+
) {
43+
- // remove any configuration that is hardcoded or included during compilation
44+
- this.webServiceUrl = 'https://api.myapp.com'
45+
+ // use globally scoped environment variables that are unique to the deployment
46+
+ this.webServiceUrl = window.env.API
47+
}
5148

52-
getData() {
53-
- return this.http.get(`${environment.API}/assets/data.json`);
54-
+ // use globally scoped environment variables that are unique to the deployment
55-
+ return this.http.get(`${window.env.API}/assets/data.json`);
49+
getUsers() {
50+
return this.http.get(`${this.webServiceUrl}/users`);
5651
}
5752
}
5853
```
5954

55+
The values for the environment _variables_ are set on an `index.html` that is unique to each environment.
56+
6057
#### Static assets must be hosted at locations that are unique and _independent of the web application environment(s)_.
6158

6259
Static assets that do not contain anything environment-specific can be built once, published to a unique location, and then used in multiple environments of the web application.
@@ -96,7 +93,7 @@ Static assets that do not contain anything environment-specific and are hosted
9693
```html
9794
<script>
9895
env = {
99-
API: 'https://api.myapp.com'
96+
API: 'https://api.myapp.com',
10097
GA: 'UA-126263119-1'
10198
}
10299
</script>
@@ -127,7 +124,8 @@ The `index.html` of most single-page web applications is typically a small docum
127124
<!-- environment variables -->
128125
<script>
129126
env = {
130-
API: 'https://api.myapp.com'
127+
API: 'https://api.myapp.com',
128+
GA: 'UA-126263119-1'
131129
}
132130
</script>
133131

0 commit comments

Comments
 (0)