1

In development, the project runs smoothly, but when I try to deploy on vercel I get the error

Error occurred prerendering page "/". FetchError: request to http://localhost:3000/api/getPageInfo failed, reason: connect ECONNREFUSED 127.0.0.1:3000

if i try to access the 'http://localhost:3000/api/getPageInfo' from any browser i can do it, its available. there are 2 more env var, besides NEXT_PUBLIC_BASE_URL, NEXT_PUBLIC_SANITY_DATASET which is = 'production' and NEXT_PUBLIC_SANITY_PROJECT_ID to access the project in sanity studio. they are all configured in vercel

NEXT_PUBLIC_BASE_URL = http://localhost:3000


import type { NextApiRequest, NextApiResponse } from "next";
import { groq } from 'next-sanity';
import { sanityClient } from "../../sanity";
import { Experience } from "../../typings";

const query = groq`
    *[_type == 'experience'] {
        ...,
        technologies[]-> 
    }
`;

type Data = {
    experiences: Experience[];
}

export default async function handler(
    req: NextApiRequest,
    res: NextApiResponse<Data>
) {
    const experiences: Experience[] = await sanityClient.fetch(query);
    res.status(200).json({ experiences })
}







import { Experience } from '../../typings';

export const fetchExperiences = async() => {
        const res = await fetch(`${process.env.NEXT_PUBLIC_BASE_URL}/api/getExperience`);
    
        const data = await res.json()
        const experiences: Experience[] = data.experiences;
        
        return experiences;
}






// ON THE INDEX PAGE (the fetch of the 5 props follows the model above)

export const getStaticProps: GetStaticProps<Props> = async () => {
  const pageInfo: PageInfo = await fetchPageInfo();
  const experiences: Experience[] = await fetchExperiences();
  const skills: Skill[] = await fetchSkills();
  const projects: Project[] = await fetchProjects();
  const socials: Social[] = await fetchSocials();

  return { 
    props: {
      pageInfo,
      experiences,
      skills,
      projects,
      socials,
    },
    revalidate: 10,
  }
}

  • If you deploy something to a public **remote** server, is it still `localhost` you are trying to reach with your browser? – Daniel W. Sep 21 '22 at 12:03
  • Is this question related https://stackoverflow.com/questions/62049721/nextjs-vercel-mongodb-fetcherror-request-to-http-localhost3000-api-gear-fail?rq=1 ? – Alex Biba Sep 21 '22 at 12:04
  • You should not call an internal API route inside `getStaticProps`. Instead, you can safely use your API logic directly. 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 Sep 23 '22 at 22:47
  • in my case I changed localhost to 127.0.0.1 and it works – Daniel Nov 13 '22 at 09:32

0 Answers0