Skip to content

Commit 4052e05

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 4052e05

2 files changed

Lines changed: 128 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: 127 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,127 @@
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+
> **Security note:** Environment files are bundled into the client-side application and are visible to anyone who can load the page. Never store sensitive information like API keys, secrets, or credentials in environment files, as they can be easily accessed by users.
19+
20+
Generate environment files using the CLI:
21+
22+
```bash
23+
ng generate environments
24+
```
25+
26+
This creates environment-specific files such as:
27+
28+
```ts
29+
// environment.ts (production)
30+
export const environment = {
31+
production: true,
32+
apiUrl: 'https://api.example.com',
33+
};
34+
```
35+
36+
```ts
37+
// environment.development.ts
38+
export const environment = {
39+
production: false,
40+
apiUrl: 'http://localhost:3000',
41+
};
42+
```
43+
44+
Import the environment where needed:
45+
46+
```ts
47+
import { environment } from '../environments/environment';
48+
49+
const apiUrl = environment.apiUrl;
50+
```
51+
52+
The Angular CLI replaces the appropriate file based on the build configuration.
53+
54+
> Changes to environment files require rebuilding the application.
55+
56+
---
57+
58+
## Runtime configuration (advanced)
59+
60+
In some scenarios, applications need to load configuration at runtime instead of build time.
61+
62+
This allows the same build artifact to be deployed across multiple environments without rebuilding.
63+
64+
A common approach is to load a JSON configuration file from the `assets` folder during application initialization.
65+
66+
### Example
67+
68+
```json
69+
// src/assets/config.json
70+
{
71+
"apiUrl": "https://api.example.com"
72+
}
73+
```
74+
75+
Load the configuration before the application starts:
76+
77+
```ts
78+
import { Injectable } from '@angular/core';
79+
import { HttpClient } from '@angular/common/http';
80+
81+
@Injectable({ providedIn: 'root' })
82+
export class AppConfigService {
83+
private config!: { apiUrl: string };
84+
85+
private readonly http = inject(HttpClient);
86+
87+
loadConfig() {
88+
return this.http.get<AppConfig>('/assets/config.json').pipe(
89+
tap(data => {
90+
this.config = data;
91+
})
92+
);
93+
}
94+
95+
get apiUrl(): string {
96+
return this.config.apiUrl;
97+
}
98+
}
99+
```
100+
101+
Register the loader during application bootstrap:
102+
103+
```ts
104+
import { provideAppInitializer, inject } from '@angular/core';
105+
106+
provideAppInitializer(() => {
107+
const config = inject(AppConfigService);
108+
return config.loadConfig();
109+
});
110+
```
111+
112+
This ensures configuration is available before the application renders.
113+
114+
> Runtime configuration is an advanced pattern and is not required for most applications.
115+
116+
---
117+
118+
## Choosing a strategy
119+
120+
| Criteria | Build-time | Runtime |
121+
|----------|-----------|---------|
122+
| Change without rebuild | No | Yes |
123+
| Startup performance | Faster | Slight delay |
124+
| Complexity | Low | Moderate |
125+
| Deployment flexibility | Limited | High |
126+
127+
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)