-
-
Notifications
You must be signed in to change notification settings - Fork 5.1k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
dynamic runtime environment variables #5100
Comments
Hi @janesser. I've transferred issue from RFC to here because it is more an enhancement discussion. Values passed to the |
I also ran into this problem, here is one of my example case: I'm using nuxtjs auth module with some OAuth API. Here is what the config looks like: https://auth.nuxtjs.org/reference/providers/github , in auth: {
strategies: {
github: {
client_id: '...', I'd like to build my app once and deploy it many times with different configuration values. This cannot be done because Here you couldn't build your app in a container and deploy it with different I hope this makes the benefits of having nuxt projects configurable at runtime a bit clearer. |
@pi0 its more about have runtime env vars like in the module that is linked. E.g. build once deploy to different environments same artifact, isntead of building each time for each env ( e.g. staging, test, production etc ) |
I see the point. And auth and axios are probably best examples for such need. However, this is a duplicate topic and issue was discussed here is a short brief of status:
Having built-in support for case (2) could be possible but potentially opens a way to critical security holes and is only usable for SSR users. Regarding case (3) also could be possible to store app config in a json file inside |
@pi0 can you link the original discussion that you refer too, please? @vhf @aldarund thanks for supporting the need. regarding the security, i am confident that docker envvars are sufficiently safe. i felt like you hit the sensible point where it came to the way that webpack4 build routine is instrumented. Probably the present behaviour is at least partially inherited. regarding possible solutions, did you check nuxt-env? by my experience it covers SSR plus non-SSR in a consistent way, not sure how it is solved internally. //cc @samtgarson ill try to collect some insight on webpack4 and eventually read through the internals of nuxt-env to give you some additional pointers. |
@janesser in the meantime open an issue for any confusing things you're hitting—I picked @pi0 all I do is use a middleware to add the Would be great to have this natively, it's currently impossible to use Nuxt in any kind of containerised, multi-environment setup with the existing native setup, or at least anywhere where the production environment could vary from the build environment. |
(non SSR and SPA are still scenarios that I don't think |
Mainly discussed in #618. We are aware of @samtgarson About security matter. Docker can't prevent one accidentally passing |
About secrets retention, check this one: samtgarson/nuxt-env#9 To wrap up, is there a way to make process.env as consumed by default from nuxt-plugins in the same way as process.$env? |
This is my solution: set dynamic runtime environment variables in so we can get it in client side and server side via // store/index.js
export const state = () => ({
env: 'development'
})
export const actions {
// run in every SSR
async nuxtServerInit (store, context) {
// read runtime environment everytimes and set to store
const env = process.env.NODE_ENV || 'development'
store.commit('SET_ENV', env)
}
}
export const mutations = {
SET_ENV (state, env) {
state.env = env
}
} Use in middleware// middleware/env.js
export default function ({store}) {
console.log(store.state.env)
} Use in page/layout/component<template>
<div>{{env}}</div>
</template>
<script>
export default {
data () {
return {
env: this.$store.state.env
}
}
}
</script> |
@pi0 @samtgarson what's the next step here? |
Definitely. The ability to configure auth, axios and apollo at runtime would be immensely helpful to people who wish to build once and deploy many times. I'd be happy to contribute if the core team agrees on what the right approach should be. |
** EDITED to add some elements for clarification of the issue the problem is that process.env.* gets replaced with "live(build-time)" placeholders during build time, anyway, I'm quite new with nuxt/vue and having to know so much internals to runtime-configure the application and having to read the (0-comments) source code of my 2 cents are that this is a serious issue, I've lost 3 hours trying to figure out why my local dev env works and my deploy env didn't , the problem was that there was two different graph of injected variables because the build process interpolated them from different build environment, I think that saying that there is no official way -to use env variables || to inject configuration- at runtime but that https://github.com/samtgarson/nuxt-env that "works" , or to use env variables at build-time (which is an huge anti-pattern in 2019 ) must be written at least here I want to add that this is a very subtle issue, because the nuxt.config.(ts|js) is run twice, at build time and at runtime, but at build time it will compile apollo/axios and other modules with env variable that are present at build-time , when you run at build time, and try to debug, the nuxt.config.js file is executed with correct env variables, but the configuration for the module that you expect to gets configured during this execution are already configured with hardcoded build-time-value... |
@pi0 I'm happy to move the module over to nuxt community if that would help the situation, let me know |
@samtgarson Let's discuss that on https://discord.nuxtjs.org/ |
Just to give some idea of a possible solution: So the basic steps for a docker deployment (on container execution) are:
I can perfectly image that these steps may be included into |
I have (had) a need for this too and I use nuxt-env (excellent module) where I can but there are places that nuxt-env can't help like configuring your API URL for the nuxt-proxy module in the I run in Docker so I decided to get a bit hacky and just use placeholders during the docker build: # Dockerfile
...some stuff
RUN \
export KEYCLOAK_CLIENT_ID='%%KEYCLOAK_CLIENT_ID%%' && \
export KEYCLOAK_HOST='%%KEYCLOAK_HOST%%' && \
export KEYCLOAK_REALM='%%KEYCLOAK_REALM%%' && \
export API_BASE_URL='%%API_BASE_URL%%' && \
yarn build
... more stuff Then in the # entrypoint.sh
find .nuxt/ \
-type f \
-name '*.js' \
-exec sed -i "s+%%KEYCLOAK_CLIENT_ID%%+${KEYCLOAK_CLIENT_ID:?}+g" '{}' \; \
-exec sed -i "s+%%KEYCLOAK_HOST%%+${KEYCLOAK_HOST:?}+g" '{}' \; \
-exec sed -i "s+%%KEYCLOAK_REALM%%+${KEYCLOAK_REALM:?}+g" '{}' \; \
-exec sed -i "s+%%API_BASE_URL%%+${API_BASE_URL:?}+g" '{}' \;
exec yarn start It's not glamorous but it gets the job done. It works for local dev ( |
Thanks for your contribution to Nuxt.js!
Issues that are labeled as |
This is still an important issue for those who'd like to build easily deployable products with Nuxt. It would be good if stalebot didn't close it IMO. |
Hi @joaquimds you can now try RuntimeConfig which also supports SPA (no need to inject from app.html) Closing the issue as it is landed to 2.13.0. Thanks again @janesser @samtgarson and all feedbacks on this issue ❤️
Will be landed soon on http/axios modules |
@pi0 RuntimeConfig is great, but it doesnt allow use to use environment variables in our Is there an open ticket for this problem? Should this one be re-opened? |
🤔 I'm not sure if I got your point... could you elaborate it? (I guess another issue would be better, since the main subject of this one was in fact solved) |
@blowsie i used a dirty sed solution in my last project with old nuxt.js version 2.11.0. And i think i know what you want.. |
Sadly this is not true, the main subject of this was immutable build artifacts, we dont have that yet, at least not in the nuxt.config |
@blowsie are you using SPA mode or Universal mode? I'm not as familiar with SPA mode enough to suggest a fix for this, but if you're using Universal mode (i.e. SSR) then using For these, I think it would be great if the Nuxt team could help nudge the major in the right direction to support runtime config, I know when people used |
@samtgarson I am indeed using universal mode. But you are right, the issues im having are with the nuxt modules, like for example nuxt-community/auth-module : nuxt-community/auth-module#713 (comment) |
Yep. I don't think there's much Nuxt can do while those modules are writing config to file other than help module authors migrate to this strategy. |
I just ran into this issue (our site runs in SSR mode, on a serverless platform). My use case: I tried to use nuxt-env (which is a great module!) but it wasn't accessible via nuxt.config.js, which is where the GTM module configuration resides in https://github.com/nuxt-community/gtm-module. This was my solution:
|
Hi @blowsie indeed as @samtgarson mentioned, it is up to module authors supporting runtime config which auth module still hasn't. @clairegraham You can try latest version of gtm-module. It supports runtime config: https://github.com/nuxt-community/gtm-module#runtime-config export default {
modules: [
'@nuxtjs/gtm'
],
publicRuntimeConfig: {
gtm: {
id: process.env.DEPLOY_TARGET == 'staging' ? 'GTM-STAGING-ID' : 'GTM-PROD-ID'
}
},
} |
Hi, |
it's still not working and not documented anywhere. It's a shame that something important like this get tossed aside for over a year already |
No, unfortunately. The problem is that i have a single build (i.e 1 docker image) that is used on multiple environments. Each user should be able to specify their own router base as an .env variable and just run the container without the need to rebuild the image.
plugins\cdn.js:
which replaces all resources on run-time! which is important but still i get a 404 page even though nuxt is able to find all the needed resources. |
@rotemrevivo91 Would you please open a feature request for supporting runtime baseURL? |
@pi0 will do :) |
Did anyone find a solution to supporting multiple environments? The Nuxt docs seem to assume that everyone is running production configs locally or at best only has dev + production. If you have local/sandbox/staging/production this runtime config doesn't seem suitable for large applications with multiple environments before production. Just getting started with Nuxt, I assume I can use .env files instead. |
I've used it to run many dynamic environments (we ran a hand-rolled version of Vercel'a preview environments with a deployment for each branch as well as production). I'd suggest you open a Discussion on the nuxt repo with your specific issues, as (apart from specific pieces of config like above) runtime config should allow you to have different config values per environment. As mentioned above, many community modules still need to adopt this feature 😕 |
We opened a new feature request #8509 (comment) |
Locking issue to avoid confusions. Runtime config for multiple envs is supported. Requested feature is for setting baseURL (router.base) after build. If something is not working as expected or need another feature please use new issue. |
While striving for immutable build artifacts we came across some possible improvement points.
First let me explain the motivation behind "immutable build artifact". Alongside of the stages like e,g, DEV, TEST, QA, UAT, PROD the configuration changes, as an example nuxt-axios API_URL and API_BROWSER_URL. Ideally this can be injected by env vars, which pretty much fits the kubernetes way of dealing with configuration.
Presently a work-around can be achieved with help of https://github.com/samtgarson/nuxt-env, which does a decent job in providing needed injection mechanism. The down-side is that no nuxt-plugin by default goes for this.$env, which drives one in a lot of customizing of plugins. Not to mention the broken upgrade ease.
Can you imagine a way to support this natively in some of the next nuxt releases?
The text was updated successfully, but these errors were encountered: