Next.js Discord

Discord Forum

Shared global per-user state

Answered
Great golden digger wasp posted this in #help-forum
Open in Discord
Great golden digger waspOP
I have multiple environments that I want to swap to dynamically without deploying multiple frontends for each. Is there any global per-user context that can be used from all 3 - server actions, server components and client components? e.g. each environment would have:

name, client_id, api_address

currently I use process.env but that requires deploying a separate frontend for each api_address... which is what I'm trying to avoid. I have NEXT_PUBLIC_API_URL env variable but I want this to be configurable at run-time and per-user instead.
Answered by B33fb0n3
you can use cookies for example. cookies can be read in server component and server actions thought cookies() and on client via third party libraries
View full answer

38 Replies

Answer
Great golden digger waspOP
Yeah I was thinking of that
Can’t cookies() be used in client components as well? I have to use 3rd party lib?
@Great golden digger wasp Can’t cookies() be used in client components as well? I have to use 3rd party lib?
no you can't. For what do you need cookies clientside? What do you want to do?
Great golden digger waspOP
I would need to set a cookie based on selected item in selectbox
And also display current app config (env name, api base)
@Great golden digger wasp I would need to set a cookie based on selected item in selectbox
why dont you set that only clientside? For what does the server know the value before any action?
@Great golden digger wasp And also display current app config (env name, api base)
Why dont you use a database that saves everything about the user (env name, api base, ...)?
Some /config endpoint
@Great golden digger wasp Some /config endpoint
yea, or server actions if the user can do something with it
@B33fb0n3 why dont you set that only clientside? For what does the server know the value before any action?
Great golden digger waspOP
My intention is to have a selectbox in the header next to logout button that allows changing current environment that would reload the app but fetching all the data from other api server (and different database)
Great golden digger waspOP
Yeah
I have multiple backends doing the same thing but using a different database
Server actions just serve as additional layer over these APIs
alright. What do you think about having this one value inside your searchParams? They can be changed and read clientside and also serverside
Great golden digger waspOP
Now if I had 5 backends running I would need to deploy 5 separate nextjs apps using different env variables
So this is what I’m trying to solve
Great golden digger waspOP
It’s really just 1 app
do these backend have a name like production developement test ...?
Great golden digger waspOP
Noo
@Great golden digger wasp It’s really just 1 app
yea, we want 1 app that can connect to 5 different backends and the user can easily switch them using one select
Great golden digger waspOP
Yes
Its really like multiple oauth apps with their client ids and secrets but each app has their own backend with database deployed
So app1-api.example.com, app2-api.example.com
oh ok, then forget the idea with the searchparams.. it would be to much work to maintain the state over route changes... I would say, that cookies are the best idea to solve it.

I think about this now:
1. Create a database table, that contains the specific keys and pair them with an id.
2. Then the user can select one of these ids (you can also fetch them directly from the database if you need to)
3. The user select an id and everything automatically changes.
Because: if the user now for example call the function authMe the authMe function (serverside) would check the value of the specific cookie for the backend and use the correct backend directly
Great golden digger waspOP
Yeah so cookies or database config it is then 🤔
I would do it like that, yea. Of course you can extend the database table even more. For example if a backend is currently in maintainence then there is a status inside the database, that it is in maintainence. Or you can configure a fallback, that is saved inside the database. Or ...
Great golden digger waspOP
Thanks i will try. Other option is to make my backend select different database dynamically and just deploy 1 backend l then I don’t have to do much in nextjs
@Great golden digger wasp Thanks i will try. Other option is to make my backend select different database dynamically and just deploy 1 backend l then I don’t have to do much in nextjs
When I integrate auth with different backends (providers), I like to make an array of providers, that contains the specific env variables for these specific providers, yea. I guess you have a more complex scenario, where you might need the database stuff
@Great golden digger waspsolved?
Great golden digger waspOP
Yeah thank you, got the idea of how to do it
@Great golden digger wasp Yeah thank you, got the idea of how to do it
happy to help. Please mark solution
Great golden digger waspOP
done