Next.js Discord

Discord Forum

I need to make dynamic API_URL path into build

Unanswered
Himalayan posted this in #help-forum
Open in Discord
Avatar
HimalayanOP
i need to make build and after that i want to change api_url and if i change into main file then all the file take that change API_URL which is the way to make that types of build

below is my step

i have one file as default route and there is i'm using backend data api like http://192.168.0.20:5555/ and this is store into config.json file like
{
"NEXT_PUBLIC_API_URL":"http://192.268.20:5555"
}

1) create build using npm run build

2) i was send .next and packge.json and config.json file and folder to my friends

3) his install package using npm i

4) after that his change config.json file 5555 to 2222 to use another server

5) and start using npm start

but every time it's call 5555 server
is there any way to change this cos at build time it's set index.html statically

6 Replies

Avatar
Sun bear
Im not sure exactly what you mean in your question but perhaps you can take a look at environment variables:

https://nextjs.org/docs/app/building-your-application/configuring/environment-variables
Avatar
HimalayanOP
is there any way to change this cos at build time it's set index.html statically but i want like if his change config.json file then every where change and it's take change API_URL.
If you want to more information let me know i'll provide as soon as possible Thanks in Advanced. 🙂
Image
i want to change this dynamically
Avatar
Little yellow ant
Without things getting messy and do a string / regex each and replace on the compiled code, no. This is one of my issues with how app router handles configuration / env variables.

What you could do is create them as runtime env variables aka remove the NEXT_PUBLIC_ portion. Then, if that needs to be made available to the client side of the app then you will have to pass it via something like a script block with a global variable. I would highly recommend using Object.freeze in this situation for that to keep it from being modified easily.
Avatar
HimalayanOP
can you give me one example ?/