9
9
* [Overview](#overview)
10
10
* [Simplest deploy possible](#simplest-deploy-possible)
11
11
* [Minimizing your payload](#minimizing-your-payload)
12
- * [Less files](#less -files)
12
+ * [Fewer files](#fewer -files)
13
13
* [Smaller files](#smaller-files)
14
14
* [Template compilation](#template-compilation)
15
15
* [Angular configuration](#angular-configuration)
@@ -28,7 +28,7 @@ a#overview
28
28
After creating your brand new Angular app you'll want to put it online for the world to see.
29
29
30
30
Your development setup is optimized for build speed and rapid iteration, but when
31
- deployment to production you'll want to optimize for loading speed and payload size.
31
+ deploying to production you'll want to optimize for loading speed and payload size.
32
32
33
33
In this chapter you'll see how to deploy your app right now, techniques to reduce your
34
34
payload size, plus Angular and server configuration.
@@ -41,7 +41,7 @@ a#overview
41
41
It is already working locally after all, so it should work live.
42
42
43
43
1. Copy over your local project folder to your server.
44
- 2. In your server, edit `index.html` to have the right `<base href="/">` tag. If you are
44
+ 2. On your server, edit `index.html` to have the correct `<base href="/">` tag. If you are
45
45
using a subfolder (e.g. `www.mysite.com/myapp/`) it should be `/myapp/` otherwise leave it as `/`.
46
46
[More on this later](#angular-configuration).
47
47
3. Configure your server to redirect requests for missing files to `index.html` instead.
@@ -50,6 +50,15 @@ a#overview
50
50
And this is all you need to publish your app!
51
51
52
52
It's not a very good production deploy though.
53
+
54
+ For one, it has a lot of files that aren't even used when serving your site to users since it has
55
+ all the development only packages.
56
+ You could cut down the number of files by doing a production install (`npm install --production`),
57
+ but only *after* compiling your Typescript files.
58
+
59
+ It also has a lot of small files. Each of those is going to take a whole round trip to get
60
+ delivered to the browser, so it's not going to be that fast to load.
61
+
53
62
There's more you can do to make our site fast for users.
54
63
55
64
.l-main-section
@@ -61,28 +70,27 @@ a#overview
61
70
There are several techniques for achieving a smaller payload for your app:
62
71
63
72
- Bundling: concatenating several modules into a single file (bundle).
64
- - Inlining: add html and css as strings inside components.
73
+ - Inlining: adding html and css as strings inside components.
65
74
- Minification: removing all unnecessary whitespace and optional tokens.
66
75
- Uglification: rewriting code to use shorter variable/function names.
67
- - Dead Code Elimination: removing code/variables that will never used.
76
+ - Dead Code Elimination: removing code/variables that are not used.
68
77
- Tree shaking: removing unused module exports.
69
78
- Ahead-of-Time (AoT) Compilation: pre-compiling Angular templates.
70
79
71
80
Each of these do different things overall, but they all work together and their effects
72
81
compound on each other.
73
82
74
- ### Less files
75
- Bundling and inlining reduce your app's load time smaller by serving less files and thus
76
- less round trips.
83
+ ### Fewer files
84
+ Bundling and inlining reduce your app's load time by serving fewer files and thus
85
+ fewer round trips.
77
86
For small files, the overwhelming majority of time it takes to get it is not spent downloading,
78
87
but rather communicating with the server and coordinating the transfer.
79
88
You can read more about resource timings in the
80
89
[Chrome DevTools Network Performance page](https://developers.google.com/web/tools/chrome-devtools/network-performance/understanding-resource-timing)
81
90
82
91
### Smaller files
83
92
Minification, Uglification and Dead Code Elimination all contribute to reduce the total size
84
- of your code to the smallest possible amount of bytes, which will result in a smaller
85
- transfer time.
93
+ of your code to the smallest possible amount of bytes, which results in a smaller transfer time.
86
94
87
95
Tree shaking also reduces the total size but does so by removing whole exports from modules.
88
96
It was popularized by [Rollup](http://rollupjs.org/) and you can read more about it in
@@ -91,15 +99,19 @@ a#overview
91
99
### Template compilation
92
100
AoT compilation reduces render time by pre-compiling Angular templates that otherwise would be
93
101
compiled at runtime.
94
- It also reduces payload size since the Angular compiler itself is no longer needed, and nor are
95
- directives that are never used in templates. This allows Tree-Shaking to further remove exports.
102
+ It also reduces payload size since the compiled app does not depend on the Angular compiler,
103
+ nor on declared directives that are never used in templates.
104
+ This allows Tree-Shaking to further remove exports.
96
105
97
106
You can read more about AoT Compilation in our [dedicated cookbook chapter](aot-compiler.html),
98
- where you'll also find instructions on how to use Rollup to do all the optimizations shown here.
107
+ where you'll also find instructions on how to use Rollup to perform all the optimizations shown here.
108
+
99
109
Another great choice is [Webpack 2](https://webpack.js.org/) together with the
100
110
[Angular Ahead-of-Time Webpack Plugin](https://github.com/angular/angular-cli/tree/master/packages/%40ngtools/webpack).
111
+ Webpack is an easy to customize, high performance bundler, and the official AoT Webpack plugin
112
+ makes it easier than ever to use AoT and lazy-loading.
101
113
102
- You can use any build system you like however, the only important thing is to optimize your build.
114
+ You can use any build system you like; the only important thing is to optimize your build.
103
115
Whatever build system you choose make sure to automate it so that you can make a production
104
116
build in a single step.
105
117
@@ -112,26 +124,26 @@ a#overview
112
124
There is an important configuration item in the Angular Router that needs to be adjusted for
113
125
different environments:
114
126
[the `base` tags `href` property](https://angular.io/docs/ts/latest/guide/router.html#!#base-href).
115
- This property tells the router how what the app root URL is so that the router can match routes.
127
+ This property tells the router what the app root URL is so that the router can match routes.
116
128
117
129
On your development server, your app is most likely served at the root: `http://localhost:3000/`.
118
130
Here you use `<base href="/">`, since `/` is the root of your app.
119
131
120
- But depending on the server setup that you use, it can also be served in a subfolder like
132
+ But depending on the server setup that you use, the app can be served from a subfolder like
121
133
`http://localhost:3000/myapp/` or `http://www.mysite.com/myapp/`, where `/myapp/` is the subfolder.
122
134
In this case you should use `<base href="/myapp/">`, since `/myapp/` is the root of your app and
123
135
not `/`.
124
136
125
- When your `base` tag is misconfigured your browser console will show several console, from
126
- missing files on wrong paths to router errors about missing routes.
137
+ When your `base` tag is misconfigured your browser console will show several errors in the console,
138
+ from missing files on wrong paths to router errors about missing routes.
127
139
128
140
### enableProdMode
129
141
130
142
You can also configure your Angular app to start on
131
143
[production mode](https://angular.io/docs/ts/latest/api/core/index/enableProdMode-function.html),
132
144
which will make it faster by disabling development specific checks.
133
145
134
- Angular apps default do development move , as you can see by the following message on the browser
146
+ Angular apps default to development mode , as you can see by the following message on the browser
135
147
console:
136
148
137
149
code-example( format ="nocode" ) .
@@ -159,13 +171,13 @@ code-example().
159
171
it to be imported directly instead of being imported via the Router.
160
172
161
173
It's important to note that your bundling configuration must take lazy loading into consideration.
162
- Since there is no ES6 import for that module, normal bundlers don't know that they should also
163
- separately bundle to module listed in the router config.
164
- You have to manually create additional bundles for each lazy loaded route .
174
+ Since there is no ES6 import for that module, bundlers don't know that they should also
175
+ separately bundle modules listed in the router config.
176
+ You have to manually create additional bundles for each lazy loaded Angular Module .
165
177
166
178
If you are using Webpack, the
167
179
[Angular Ahead-of-Time Webpack Plugin](https://github.com/angular/angular-cli/tree/master/packages/%40ngtools/webpack)
168
- will automatically recognize lazy routes .
180
+ will automatically recognize lazy loaded NgModules .
169
181
170
182
171
183
.l-main-section
@@ -174,12 +186,11 @@ code-example().
174
186
175
187
Angular apps are
176
188
[Single Page Applications](https://en.wikipedia.org/wiki/Single-page_application) (SPAs),
177
- which makes them the perfect candidates to be served by simple static HTML server.
189
+ which makes them the perfect candidates to be served by a simple static HTML server.
178
190
No preprocessors required!
179
191
180
192
There is only one configuration item required of a server hosting an Angular app:
181
- it needs to be able to fallback to `index.html` when being asked for a file the server
182
- does not have.
193
+ it needs to fallback to `index.html` when being asked for a file the server does not have.
183
194
184
195
### Why fallback to `index.html`?
185
196
@@ -196,8 +207,8 @@ code-example().
196
207
Then you click the link for `admin/` and the your address bar will change
197
208
to `www.my-awesome-app.com/admin/` and you're taken to the admin page.
198
209
199
- But if you don't have fallback to `index.html` configured and refresh the page, you will
200
- get a `404 - Not Found` page !
210
+ If you then refresh the page, you will get a `404 - Not Found` page unless you have the fallback
211
+ to `index.html` configured !
201
212
Which doesn't make a lot of sense since you just saw that the admin page was there.
202
213
203
214
When you first clicked the link to `admin/` there was no server request for
@@ -210,17 +221,17 @@ code-example().
210
221
211
222
That is why the server needs to be configured to fallback to `index.html`.
212
223
When a user asks the server for any route (that isn't a file), you want the server to *always*
213
- return default page that boots up Angular.
214
- After Angular is loaded and bootstrapped the router will look at the address bar and take
215
- the user where he needs to go .
224
+ return the default page that boots up Angular.
225
+ After Angular is loaded and bootstrapped the router looks at the address bar and takes
226
+ the user to the appropriate route .
216
227
217
228
### Fallback configuration examples
218
229
219
230
Different servers are configured in different ways so there is no single configuration that
220
231
works everywhere.
221
232
222
233
Following are configurations for some of the most popular servers.
223
- The list is by no means exhausting , but should provide you with a good starting point.
234
+ The list is by no means exhaustive , but should provide you with a good starting point.
224
235
225
236
#### Development servers
226
237
@@ -310,7 +321,7 @@ code-example(format=".").
310
321
These are not due to the SPA application itself but rather due to the configuration of APIs with
311
322
which the SPA communicates.
312
323
313
- These APIs need to send the `Access-Control-Allow-Origin: *` header on their HTTP responses,
314
- otherwise the SPA will not be able to call them.
324
+ If these APIs are on different domains, they need to send the `Access-Control-Allow-Origin: *`
325
+ header on their HTTP responses otherwise the SPA will not be able to call them.
315
326
You can read more about how to enable CORS for specific servers in
316
327
[enable-cors.org](http://enable-cors.org/server.html)
0 commit comments