https://stackoverflow.com/questions/65546341/how-to-handle-fetch-urls-with-nextjs
I tried it that way but the environmental variable can't be found
#Fetch API configuration
35 messages · Page 1 of 1 (latest)
Why not just create a helper util that does that?
i don't want the URL hardcoded
As in, you don't want it in committed in code?
yeah, i want a config or environment variable
Then, use one
this does not work
How so?
the variable is undefined if i try it
in my test instance, I tried it just like in the stackoverflow example:
const res = await fetch(`${process.env.ROOT}/api/users`)
(in a get ServerSideProps)
and I defined in the next.config.js:
module.exports = {
env: {
ROOT: process.env.NODE_ENV === 'production' ? `https://vercel.com/myaccount/myappname` : `http://localhost:3000`
}
};
Hmmm, it shouldn't be undefined
Can you try a different env name, maybe be more verbose
Also, how did you verify it is undefined?
i checked the network log via the browser's dev tools, it just added the fetch path to the nextjs url (as it is by default)
Can you share please?
What are you expecting to happen?
Or I should say, what is the expected full url to fetch?
so my nextjs app is on localhost:3000
and the api on localhost:8102
and it just added the fetch path to the nextjs app url and not the api url configured in the variable
Should you not use localhost:8102 in your config?
The config you shared has port 3000
yeah i do, i just quoted it from stackoverfloew
yess because it's from the stackoverfloew example
wait i will send my files
Show me, your config please
🙂
but ty :)
magic