Define runtime config programmatically on server start
Describe the feature
I would like to be able to define runtime config programmatically on server start. In Nuxt 2, I used to populate publicRuntimeConfig with my custom function, which returned a carefully tailored object based on environment variables and possibly other environmental conditions. It also validated and normalized config values as I see fit.
In Nuxt 3, nuxt.config.ts is a build-time dependency, and the raw value of runtimeConfig is written into .output/server/chunks/nitro/node-server.mjs. On server start, it gets overridden from env vars and then immediately frozen. This is probably fine for simpler scenarios but it is missing the degree of freedom Nuxt 2 allowed.
For example, if I use this runtime config:
export default defineNuxtConfig({
runtimeConfig: {
public: { mediaUrl: '' }
}
})
and run the server with NUXT_PUBLIC_MEDIA_URL=garbage I would like to be able to catch that on server start and prevent further client-side runtime crashes. In particular, I would like to prevent users running the server without providing any media URL at all. I would also like to normalize ending slash in the provided URL.
Another example, if I use this runtime config:
export default defineNuxtConfig({
runtimeConfig: {
public: { nonPublicServiceApiKey: '' }
}
})
and start the server without env var overrides, I would like to have nonPublicServiceApiKey never end up in the client-side runtime config at all (not even as an empty string).
All and all, I just want to be able to call some kind of optional hook here in node-server.mjs:
overrideConfig(_runtimeConfig);
// <--------- here
const config$1 = deepFreeze(_runtimeConfig);
const useRuntimeConfig = () => config$1;
Additional information
- [X] Would you be willing to help implement this feature?
- [ ] Could this feature be implemented as a module?
Final checks
- [X] Read the contribution guide.
- [X] Check existing discussions and issues.