Skip to content

Commit 4a9f37a

Browse files
committed
docs(docs-infra): add environment runtime configuration
Add a new reference explaining Angular environment configuration strategies, including both build-time and runtime approaches. Register the reference in SKILL.md so it is discoverable by the skill system.
1 parent 09b9a62 commit 4a9f37a

2 files changed

Lines changed: 126 additions & 0 deletions

File tree

‎skills/dev-skills/angular-developer/SKILL.md‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -127,3 +127,4 @@ When working with Angular tooling, consult the following references:
127127

128128
- **Angular CLI**: Creating applications, generating code (components, routes, services), serving, and building. Read [cli.md](references/cli.md)
129129
- **Angular MCP Server**: Available tools, configuration, and experimental features. Read [mcp.md](references/mcp.md)
130+
- **Environment Configuration**: Strategies for build-time and runtime configuration. Read [environment-configuration.md](references/environment-configuration.md)
Lines changed: 125 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,125 @@
1+
# Environment configuration
2+
3+
## Configuration strategies
4+
5+
Angular supports two main configuration strategies:
6+
7+
- **Build-time configuration** using environment files
8+
- **Runtime configuration** by loading values at application startup
9+
10+
Choose the approach based on your deployment requirements.
11+
12+
---
13+
14+
## Build-time configuration
15+
16+
Environment files define configuration values that are replaced at build time.
17+
18+
Generate environment files using the CLI:
19+
20+
```bash
21+
ng generate environments
22+
```
23+
24+
This creates environment-specific files such as:
25+
26+
```ts
27+
// environment.ts (production)
28+
export const environment = {
29+
production: true,
30+
apiUrl: 'https://api.example.com',
31+
};
32+
```
33+
34+
```ts
35+
// environment.development.ts
36+
export const environment = {
37+
production: false,
38+
apiUrl: 'http://localhost:3000',
39+
};
40+
```
41+
42+
Import the environment where needed:
43+
44+
```ts
45+
import { environment } from '../environments/environment';
46+
47+
const apiUrl = environment.apiUrl;
48+
```
49+
50+
The Angular CLI replaces the appropriate file based on the build configuration.
51+
52+
> Changes to environment files require rebuilding the application.
53+
54+
---
55+
56+
## Runtime configuration (advanced)
57+
58+
In some scenarios, applications need to load configuration at runtime instead of build time.
59+
60+
This allows the same build artifact to be deployed across multiple environments without rebuilding.
61+
62+
A common approach is to load a JSON configuration file from the `assets` folder during application initialization.
63+
64+
### Example
65+
66+
```json
67+
// src/assets/config.json
68+
{
69+
"apiUrl": "https://api.example.com"
70+
}
71+
```
72+
73+
Load the configuration before the application starts:
74+
75+
```ts
76+
import { Injectable } from '@angular/core';
77+
import { HttpClient } from '@angular/common/http';
78+
79+
@Injectable({ providedIn: 'root' })
80+
export class AppConfigService {
81+
private config!: { apiUrl: string };
82+
83+
private readonly http = inject(HttpClient);
84+
85+
loadConfig() {
86+
return this.http.get<AppConfig>('/assets/config.json').pipe(
87+
tap(data => {
88+
this.config = data;
89+
})
90+
);
91+
}
92+
93+
get apiUrl(): string {
94+
return this.config.apiUrl;
95+
}
96+
}
97+
```
98+
99+
Register the loader during application bootstrap:
100+
101+
```ts
102+
import { provideAppInitializer, inject } from '@angular/core';
103+
104+
provideAppInitializer(() => {
105+
const config = inject(AppConfigService);
106+
return config.loadConfig();
107+
});
108+
```
109+
110+
This ensures configuration is available before the application renders.
111+
112+
> Runtime configuration is an advanced pattern and is not required for most applications.
113+
114+
---
115+
116+
## Choosing a strategy
117+
118+
| Criteria | Build-time | Runtime |
119+
|----------|-----------|---------|
120+
| Change without rebuild | No | Yes |
121+
| Startup performance | Faster | Slight delay |
122+
| Complexity | Low | Moderate |
123+
| Deployment flexibility | Limited | High |
124+
125+
Use build-time configuration for most applications, and runtime configuration when you need to deploy the same build across multiple environments.

0 commit comments

Comments
 (0)