N
Nuxt3mo ago
Domme

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
Domme
DommeOP3mo ago
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 frontend
Aman Desai
Aman Desai3mo ago
In 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
Aman Desai
Aman Desai3mo ago
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
Domme
DommeOP3mo ago
Could you show me a code example real quick where you read an env variable you fetch via the plugin?
Domme
DommeOP3mo ago
And you do not use the runtime config at all right? Like you don't set it dynamically?
Domme
DommeOP3mo ago
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
Aman Desai
Aman Desai3mo ago
in that case, it is best to use my approach
Domme
DommeOP3mo ago
Ah, but you don't can't overwrite the runtimeconfig in your plugin?
Aman Desai
Aman Desai3mo ago
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
Want results from more Discord servers?
Add your server