How i can dynamically set env variable?
Unanswered
Sloth bear posted this in #help-forum
Sloth bearOP
So i have env variable set in my next.config.js file like:
This variable is used in various components to connect my database. I want this variable to be changed depending which subdomain user is having, to connect into correct database. Whats the best way to do this?
env: {
DB_HOST: 'http://localhost:4000',
},This variable is used in various components to connect my database. I want this variable to be changed depending which subdomain user is having, to connect into correct database. Whats the best way to do this?
19 Replies
Snowshoe
you can't change an environment variable via code. You could use DB_HOST as a starting point, and then change it depending on the subdomain, or store that variants in your database
@Snowshoe you can't change an environment variable via code. You could use DB_HOST as a starting point, and then change it depending on the subdomain, or store that variants in your database
Sloth bearOP
how would you do that, i mean changing it depending on the subdomain?
Sloth bearOP
Anyone please? I have tried to solve this for so long now
@Sloth bear Anyone please? I have tried to solve this for so long now
Kokoni
Hey:) the answer is: don't rely on env vars! Parse out the values of the subdomain manually and put it in some sort of store to match against.
So for instance you might have a db table for all of your websites called "Global" and a table in global called "Config." The ID row of Config is the subdomain, and the cols of config are various bits of information such as... "database_url"
@Kokoni So for instance you might have a db table for all of your websites called "Global" and a table in global called "Config." The ID row of Config is the subdomain, and the cols of config are various bits of information such as... "database_url"
Sloth bearOP
Thanks for your answer. Yeah seems like env variables here are not an option, but parsing subdomain or current url is the thing im struggling at the moment, because api urls are used in client and server components
Sloth bearOP
Still havent solved this, any assistance is appreciated
@Sloth bear Still havent solved this, any assistance is appreciated
Brown bear
could you do something like this:
process.env.DB_HOST + subdomain
and dynamically change the subdomain
process.env.DB_HOST + subdomain
and dynamically change the subdomain
ex:
'http://localhost:4000' + '/home'Have you used useRouter() and get the location?
'use client'
import { usePathname } from 'next/navigation'
export default function ExampleClientComponent() {
const pathname = usePathname()
const dbPath = process.env.DB_HOST + pathname
return(
//your component here
)
}Kokoni
Usepathname won't work for the whole URL, it'll only return the URL segments.
So it won't work for subdomains.
Brown bear
hmm
Kokoni
Userouter should get that
Getting URL from server actions/server components is an antipattern from what I remember
@Kokoni Userouter should get that
Brown bear
it might, i'm not sure