Sub topic 14
Building for Production
This chapter assumes you have finished the earlier chapters. You should have the task list app running with ng serve, a TaskService backed by signals, and unit tests that pass. You should also be comfortable with npm scripts and basic terminal use.
Development build versus production build
ng serve compiles the app in memory and rebuilds it on every change. It favors fast feedback. The output is large, readable, and not meant for real users.
ng build writes files to disk and, by default, uses the production configuration. In a new project, that configuration does these things:
- Ahead-of-time (AOT) compilation: templates are compiled to JavaScript at build time, not in the browser.
- Minification: whitespace is removed and names are shortened.
- Tree shaking: code that nothing imports is dropped from the bundle.
- Output hashing: file names include a content hash, such as
main-A1B2C3D4.js. A changed file gets a new name, so browsers and CDNs can cache files for a long time without serving stale code. - Budgets: size limits that warn or fail the build when a bundle grows too large.
Running the build
Run the tests first, then build:
ng test --watch=false
ng buildThe --watch=false flag makes the test run exit after one pass instead of waiting for changes. This is useful in scripts and CI. Stop the build if the tests fail.
The build prints a table of the files it produced and their sizes. By default the output goes to dist/task-list/. Recent Angular versions put the deployable files in the browser subfolder, so dist/task-list/browser/ holds index.html, the hashed JavaScript files, and the stylesheet.
Budgets
Budgets live in angular.json under the production configuration. A typical entry looks like this:
{
"budgets": [
{ "type": "initial", "maximumWarning": "500kB", "maximumError": "1MB" },
{ "type": "anyComponentStyle", "maximumWarning": "2kB", "maximumError": "4kB" }
]
}The initial budget covers the code the browser must load before the app starts. If it passes maximumWarning, the build prints a warning. If it passes maximumError, the build fails. Treat a failure as a prompt to look at what you added, not as a reason to raise the limit.
Configuration for other environments
The Angular CLI uses named build configurations. To build with development settings, which include source maps and no optimization, select that configuration:
ng build --configuration developmentIf your app needs different values per environment, such as the API address behind /api/tasks, avoid editing code before each build. Keep the base path relative, as in the earlier chapter, and let the web server or a reverse proxy route /api to the backend.
Serving the result
The output is static files, so any static host can serve it. Two points matter for the router you set up earlier:
- Fallback to
index.html. The app uses client-side routing. If a user opens/tasks/3directly, the server looks for a file at that path and finds nothing. Configure the server to returnindex.htmlfor unknown paths, and the Angular router then takes over. - Cache headers. Hashed files can be cached for a long time.
index.htmlshould not be, because it points to the current hashed file names.
To check the build locally before deploying, serve the folder with a simple static server:
npx http-server dist/task-list/browser -p 8080 --proxy http://localhost:8080?The --proxy option makes the server answer unknown paths with index.html, which imitates the fallback rule above. Open http://localhost:8080/tasks/1 directly to confirm that the detail route loads.
Common errors
| Symptom | Likely cause | Fix |
|---|---|---|
| Build fails with a budget error | A bundle passed `maximumError` | Find what grew, for example a large library import, and reduce it |
| Deployed app shows a blank page | Wrong base path | Set `--base-href` to the folder the app is served from |
| Refreshing on `/tasks/3` gives 404 | No fallback to `index.html` | Add a rewrite rule on the server |
| Users see old code after a deploy | `index.html` cached too long | Serve `index.html` with no-cache headers |
| Build fails on type errors that `ng serve` allowed | Stricter checks in the full build | Fix the reported types |
For an app served from a subfolder, pass the base path at build time:
ng build --base-href /apps/tasks/Exercise
A user opens https://example.com/tasks/3 directly and gets a 404 from the server, although the same link works when clicked inside the app. Why does this happen, and what is the fix?
Show answer
Inside the app, the Angular router handles the URL in the browser without a server request. A direct visit sends the request to the server, which has no file at /tasks/3. Configure the server to return index.html for paths that match no file. The router then reads the URL and shows the detail component.
Exercise
Why can hashed JavaScript files be cached for a long time while index.html should not be?
Show answer
The hash in a file name changes whenever its content changes, so a cached copy is never stale under its name. index.html keeps the same name but references the current hashed files, so a cached copy would keep pointing to old bundles after a deploy.