You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Ability to set environment/configuration values at runtime instead of buildtime #58433
Include one of the existing user-created implementations described above in Angular with suitable documentation.
Or, at least, pick one of the above and link to it from the Angular documentation.
Alternatives considered
Use one of the existing user-created implementations.
Reacted by Marcelo Luz, Ilya Borisov, Marvin Heilemann, Scott Langley, Devis Battisti, Filippo Muscolino, marcobottaro, Alberto Manfrinati, llucasshenrique, João Dias and 18 more
Is dependency injection a suitable answer to this use case? DI is the typical way of configuring applications at runtime, and DI tokens can be created which default to values from environments but are overridable via any desired mechanism.
I'm not totally convinced this is a use case Angular can meaningfully contribute to.
All the blogs/tools you reference use a similar strategy: On app bootstrap, request a JSON file from some URL.
There are two kinds of builds relevant here:
Static builds (CSR, SSG)
Server builds (SSR)
The former does not have a hook to provide a JSON configuration at runtime. We have assets, but those are specified at build time. Once the app is built, Angular does not participate in the serving process. The app could be deployed on any static file server. There's no way for Angular to indicate which configuration to use for a specific environment. Any API we provide here can't help with that particular problem, so I don't think there's much we can do for CSR apps.
The latter has a little more structure. Since Angular owns the SSR server, we do have a hook to serve some extra content. I believe we already have TransferCache for sending data like this between the client and server and is slightly more optimal than making an independent network request on page load.
There are two potential ways to make this more ergonomic:
First, we could make a provider on the server for loading the runtime configuration.
One clear downside with this is that it's not automatically typed. RUNTIME_CONFIG will need to be typed unknown and hard casted at all usages. Angular can insert the data for you, but we don't statically know the type. The way around that would be to manually create your own injection token of type MyRuntimeConfig and resolve it to RUNTIME_CONFIG, but that requires extra setup in your project and Angular can't do it for you.
Second, we could go one step further and automatically configure a default provideRuntimeConfiguration(/* ... */) implementation which reads from process.env. This is risky from a security perspective, since it could easily leak sensitive environment data from the server not intended to be public. A filter on these environment variables matching some configuration like PUBLIC_* could mitigate this, but there is always a risk of accidentally sending data you don't intend. Also the object would need to be typed Record<string, string>, so it has a similar problem to previous approach. If we went with the first approach, you could pretty easily build this second one on top of it with whatever semantics you want, so I'm not sure there's a ton of value in Angular going this extra step.
We'll discuss this within the team if this is worth pursing. I'm not totally convinced it's all that much better than the current state of affairs (aside from the TransferCache optimization), but this might be worth it just to have a singular API we can point to with a short doc of how to use it rather than asking users to dig through community posts and manually configure their server to do the right thing (though they're gonna have to do that anyways for CSR builds).
Reacted by Samuel Fernández, Patric Steiner and Jaime Burgos
I agree with @dgp1130's point, especially about the reasons for avoiding process.env. However, a minor clarification: if the HTTP request is made using Angular's HTTP client, the data is automatically stored in the transfer state, so no additional optimization is necessary.
This feature request is now candidate for our backlog! In the next phase, the community has 60 days to upvote. If the request receives more than 20 upvotes, we'll move it to our consideration list.
You can find more details about the feature request process in our documentation.
We've had some more discussion about this and think it's something which is worth considering at least. provideRuntimeConfig is something which can make sense in a CSR app (though I personally think it's much more valuable in an SSR app). However it's probably more of a core framework problem than a tooling problem, so I'll move this out of CLI. I think this is just something we'll need to prioritize based on user interest. So if this is a valuable use case for you, please consider upvoting the issue and/or sharing your particular use case.
A common use case we have is deploying the Angular client under different paths, so the baseHref and API URL are dynamic (the latter because we don't use a proxy). Our current solution is to patch index.html and provide the API URL as an asset, requiring the use of APP_INITIALIZER and creating timing issues for services that have already been constructed.
Ideally, a runtime config would be included in the initial bundle, maybe by patching data into a script tag of index.html. The challenge here is to make it as simple as possible to replace variables, since Angular does not control the server shipping the JS.
Activity
alxhub commentedon Oct 17, 2024
Is dependency injection a suitable answer to this use case? DI is the typical way of configuring applications at runtime, and DI tokens can be created which default to values from
environmentsbut are overridable via any desired mechanism.dgp1130 commentedon Oct 17, 2024
I'm not totally convinced this is a use case Angular can meaningfully contribute to.
All the blogs/tools you reference use a similar strategy: On app bootstrap, request a JSON file from some URL.
There are two kinds of builds relevant here:
The former does not have a hook to provide a JSON configuration at runtime. We have
assets, but those are specified at build time. Once the app is built, Angular does not participate in the serving process. The app could be deployed on any static file server. There's no way for Angular to indicate which configuration to use for a specific environment. Any API we provide here can't help with that particular problem, so I don't think there's much we can do for CSR apps.The latter has a little more structure. Since Angular owns the SSR server, we do have a hook to serve some extra content. I believe we already have
TransferCachefor sending data like this between the client and server and is slightly more optimal than making an independent network request on page load.There are two potential ways to make this more ergonomic:
First, we could make a provider on the server for loading the runtime configuration.
This could automatically insert it into
TransferCacheand then make it available on the client via a provider:One clear downside with this is that it's not automatically typed.
RUNTIME_CONFIGwill need to be typedunknownand hard casted at all usages. Angular can insert the data for you, but we don't statically know the type. The way around that would be to manually create your own injection token of typeMyRuntimeConfigand resolve it toRUNTIME_CONFIG, but that requires extra setup in your project and Angular can't do it for you.Second, we could go one step further and automatically configure a default
provideRuntimeConfiguration(/* ... */)implementation which reads fromprocess.env. This is risky from a security perspective, since it could easily leak sensitive environment data from the server not intended to be public. A filter on these environment variables matching some configuration likePUBLIC_*could mitigate this, but there is always a risk of accidentally sending data you don't intend. Also the object would need to be typedRecord<string, string>, so it has a similar problem to previous approach. If we went with the first approach, you could pretty easily build this second one on top of it with whatever semantics you want, so I'm not sure there's a ton of value in Angular going this extra step.We'll discuss this within the team if this is worth pursing. I'm not totally convinced it's all that much better than the current state of affairs (aside from the
TransferCacheoptimization), but this might be worth it just to have a singular API we can point to with a short doc of how to use it rather than asking users to dig through community posts and manually configure their server to do the right thing (though they're gonna have to do that anyways for CSR builds).Lindsor commentedon Oct 17, 2024
dgp1130 commentedon Oct 17, 2024
Lindsor commentedon Oct 17, 2024
dgp1130 commentedon Oct 17, 2024
alan-agius4 commentedon Oct 18, 2024
I agree with @dgp1130's point, especially about the reasons for avoiding
process.env. However, a minor clarification: if the HTTP request is made using Angular's HTTP client, the data is automatically stored in the transfer state, so no additional optimization is necessary.angular-robot commentedon Oct 18, 2024
This feature request is now candidate for our backlog! In the next phase, the community has 60 days to upvote. If the request receives more than 20 upvotes, we'll move it to our consideration list.
You can find more details about the feature request process in our documentation.
dgp1130 commentedon Oct 30, 2024
We've had some more discussion about this and think it's something which is worth considering at least.
provideRuntimeConfigis something which can make sense in a CSR app (though I personally think it's much more valuable in an SSR app). However it's probably more of a core framework problem than a tooling problem, so I'll move this out of CLI. I think this is just something we'll need to prioritize based on user interest. So if this is a valuable use case for you, please consider upvoting the issue and/or sharing your particular use case.grubeninspekteur commentedon Nov 16, 2024
A common use case we have is deploying the Angular client under different paths, so the baseHref and API URL are dynamic (the latter because we don't use a proxy). Our current solution is to patch index.html and provide the API URL as an asset, requiring the use of
APP_INITIALIZERand creating timing issues for services that have already been constructed.Ideally, a runtime config would be included in the initial bundle, maybe by patching data into a script tag of
index.html. The challenge here is to make it as simple as possible to replace variables, since Angular does not control the server shipping the JS.kyubisation commentedon Nov 16, 2024
@grubeninspekteur That's the approach https://www.npmjs.com/package/angular-server-side-configuration takes, which provides a native binary and examples for nginx. I wrote this library some years ago, as we had exactly these requirements of having the configuration available synchronously.