0

I'm trying to host a Next.js project using vercel and some api calls in getStaticProps() uses api folder inside next.js pages folder. and this function also needs a full url so I can't provide only /api/get-products. insted I have to provide something like http:localhost:3000/api/get-products.

getStaticProps() call

URL here

this is the URL that needs to change

What should I do to make this build successfully on vercel. because if I run npm run build. It builds sucessfully.

this is the error i'm getting on Vercel

error

Yasas Lowe
  • 91
  • 1
  • 6
  • Please make sure to post code and errors as text directly to the question (and [not as images](https://meta.stackoverflow.com/questions/285551)), and [format them appropriately](https://stackoverflow.com/help/formatting). – juliomalves Oct 22 '22 at 11:20
  • 1
    You should not call an internal API route inside `getStaticProps`. Instead, use your API logic directly in `getStaticProps`. See [Fetch error when building Next.js static website in production](https://stackoverflow.com/questions/66202840/fetch-error-when-building-next-js-static-website-in-production). – juliomalves Oct 22 '22 at 11:22

1 Answers1

0

I believe on the 3rd image you show us the content of the .env file in your IDE. Most probably, you have not defined such an env variable in Vercel. Here you can find info about that: https://vercel.com/docs/concepts/projects/environment-variables

Looking at the error, I'd assume you have committed the .env file with exactly the same value as on the image. Hence if you haven't redefined the variable in Vercel, it is taken from the .env file from your codebase.

Maciej Dybek
  • 186
  • 8
  • I tried that. even when I add the environment variables there. same error comes. problem is How to point same url ? – Yasas Lowe Oct 20 '22 at 16:16
  • In your .env file set NEXT_PUBLIC_VERCEL_URL=${VERCEL_URL} This should do the thing. And for local development create .env.local with NEXT_PUBLIC_VERCEL_URL=http://localhost:3000 – Maciej Dybek Oct 21 '22 at 10:35