Request env variables on application startup from backend
Hi, I want to request environment variables once my frontend loads. I have a java backend where all the configuration is located. Is there any specific hook available which I could use to load the env variables or is the best option to just send a request in my
app.vue
entry point?11 Replies
Yeah currently I have the env variables in the frontend which are required. But some are duplicated because they are also needed in the backend. Therefore I now want to request them from the backend. And now I'm basically asking what's the best way to request them. Is there any specific listener/hook I can use or do I just request them once in my
app.vue
Would be nice if it even is possible no load them once in a while with the nuxt/vue server so I don't even have to make a dedicated request from the frontendIn Vidur, I wrote a plugin to fetch env from API call and setup as state to be accessible throughout the application.
https://github.com/profilecity/vidur/blob/main/app/plugins/setup-state.ts
GitHub
vidur/app/plugins/setup-state.ts at main · profilecity/vidur
Vidur - Recruiting OS for forward thinking companies. - profilecity/vidur
but setting in plugin is the recommended way
since plugins run before any of the UI logic runs
so you can get consistent state
Yes ofcource, when you can't do that
Could you show me a code example real quick where you read an env variable you fetch via the plugin?
this is one example: https://github.com/profilecity/vidur/blob/main/app/composables/remote-asset.ts
And you do not use the runtime config at all right? Like you don't set it dynamically?
I use runtime config too, for some things, see https://github.com/profilecity/vidur/blob/main/app/components/admin/Sidebar.vue
Well, that's kind of what I am trying to do. I don't care whether it is actually dynamic set or when the frontend starts. But I want to fetch the config from the Java backend. So my frontend only uses public variables
in that case, it is best to use my approach
Ah, but you don't can't overwrite the runtimeconfig in your plugin?
never do that, it will lead to weird states
do one thing, inside the plugin, read from runtime config, and set to state
then modify the state
this happens in my case as well
then read from that state everywhere