I am trying to deploy my nextjs web app to vercel.
When I deploy my site, I get this:
> Build error occurred
FetchError: request to http://localhost:3000/api/products failed, reason: connect ECONNREFUSED 127.0.0.1:3000
Here I am fetching data on localhost:3000
, it works when npm run dev
.
But now I want to build and deploy my project, I cannot fetch from localhost
anymore, and I don't know my domain name yet. So how do I fetch data from my API route when vercel is building my site?
export const getStaticPaths = async () => {
const res = await fetch("http://localhost:3000/api/products");
const products = await res.json();
const paths = products.map((product) => ({
params: { productId: product.id },
}));
return {
paths: paths,
fallback: false,
};
};
export const getStaticProps = async (context) => {
const res = await fetch(
`http://localhost:3000/api/products/${context.params.productId}`
);
const product = await res.json();
return {
props: {
product,
},
};
};