Build & Deploy
ng serve is for development: it keeps everything in memory, adds debugging helpers and rebuilds on each save. To put your app on the internet you create a production build — a folder of optimised static files that any web server can host.
Creating a production build
ng buildInitial chunk files | Names | Raw size | Estimated transfer size
main-5XKQ2M7B.js | main | 214.36 kB | 58.12 kB
styles-6WUUEJLS.css | styles | 1.84 kB | 612 bytes
polyfills-B6TNHZQ6.js | polyfills | 34.58 kB | 11.32 kB
| Initial total | 250.78 kB | 70.05 kB
Lazy chunk files | Names | Raw size | Estimated transfer size
chunk-PA4LQ2DK.js | tasks | 18.90 kB | 5.41 kB
chunk-7JZ3N5WE.js | sales-chart | 96.12 kB | 28.77 kB
Application bundle generation complete. [6.381 seconds]
Output location: /home/dev/task-board/dist/task-board
What the production configuration does:
| Optimisation | Effect |
|---|---|
| Ahead-of-time (AOT) compilation | Templates are compiled to JavaScript at build time |
| Tree-shaking | Unused code (including unused Angular features) is removed |
| Minification | Shorter names, no whitespace |
| Code splitting | Lazy routes and @defer blocks become separate chunks |
| Content hashes in file names | main-5XKQ2M7B.js changes only when the content changes, so browsers can cache files forever |
The files to upload are in dist/<project>/browser/.
Budgets
angular.json contains budgets that warn or fail the build when bundles grow too large:
{
"budgets": [
{ "type": "initial", "maximumWarning": "500kB", "maximumError": "1MB" },
{ "type": "anyComponentStyle", "maximumWarning": "4kB", "maximumError": "8kB" }
]
}Valid JSON: object with 1 key
{
"budgets": [
{
"type": "initial",
"maximumWarning": "500kB",
"maximumError": "1MB"
},
{
"type": "anyComponentStyle",
"maximumWarning": "4kB",
"maximumError": "8kB"
}
]
}
Budgets turn "the app got slow" into a visible build warning. The check itself is simple — compare sizes against thresholds:
interface Budget { type: string; warnKb: number; errorKb: number; }
function checkBudget(b: Budget, actualKb: number): string {
if (actualKb > b.errorKb) return `ERROR ${b.type}: ${actualKb} kB exceeds ${b.errorKb} kB`;
if (actualKb > b.warnKb) return `WARNING ${b.type}: ${actualKb} kB exceeds ${b.warnKb} kB`;
return `OK ${b.type}: ${actualKb} kB`;
}
const initial: Budget = { type: 'initial', warnKb: 500, errorKb: 1000 };
for (const size of [250.78, 612.4, 1204.9]) {
console.log(checkBudget(initial, size));
}OK initial: 250.78 kB
WARNING initial: 612.4 kB exceeds 500 kB
ERROR initial: 1204.9 kB exceeds 1000 kB
Environment-specific configuration
Values that differ between development and production (API URLs, feature flags) can live in environment files. Generate them with ng generate environments:
// src/environments/environment.ts (production)
export const environment = { production: true, apiUrl: 'https://api.example.com' };
// src/environments/environment.development.ts
export const environment = { production: false, apiUrl: 'http://localhost:3000' };The CLI swaps the file through fileReplacements in angular.json, so your code always imports ./environments/environment.
Note: Everything in a front-end bundle is public. Never put secrets (private API keys, database passwords) in environment files.
Deploying the static files
An Angular app without server-side rendering is just static files, so it runs on any static host: Firebase Hosting, Netlify, Vercel, GitHub Pages, Cloudflare Pages, an S3 bucket, or nginx.
One rule matters for routing: the server must return index.html for every unknown path, so that a reload on /tasks/42 loads the app instead of a 404. For nginx:
location / {
try_files $uri $uri/ /index.html;
}If the app is served from a sub-folder, build with a matching base href:
ng build --base-href /task-board/Application bundle generation complete. [6.102 seconds]
Output location: /home/dev/task-board/dist/task-board
ng deploy
Many hosts publish a CLI builder so deployment is one command. After ng add for the host's package, run:
ng deploy📦 Building "task-board"
Application bundle generation complete. [6.244 seconds]
🚀 Uploading via git, please wait...
🌟 Successfully published via angular-cli-ghpages! Have a nice day!
Server-side rendering (optional)
ng add @angular/ssr adds server-side rendering and prerendering. The first HTML is generated on the server (or at build time), which improves first paint and SEO. The output then contains a server/ folder that needs a Node.js host — or, for fully prerendered sites, it remains static.
Tip: Before deploying, run
ng buildlocally and serve thebrowserfolder with any static server (for examplenpx http-server dist/task-board/browser) to catch production-only problems.
Test yourself
Which folder contains the files you upload to a static host?
- src/
- node_modules/
- dist/<project>/browser/
- .angular/cache/
Why must the server return index.html for unknown paths?
- For SEO only
- So that reloading a deep link like /tasks/42 loads the app and lets the Angular router handle it
- To compress files
- It is not necessary
What happens when the initial bundle exceeds the maximumError budget?
- Nothing
- A warning is logged
- The build fails
- The bundle is automatically split