abdulrcs / abdulrahman.id

⚡ Dynamic Portfolio Website built with Next.js, Chakra UI and Contentful!
https://abdulrahman.id
MIT License
218 stars 27 forks source link

FetchError: request to http://localhost:3000/api/views/[BLOG_SLUG] failed, reason: connect ECONNREFUSED 127.0.0.1:3000 #12

Closed mdrhmn closed 6 months ago

mdrhmn commented 2 years ago

I faced the following error when deploying the project to Vercel:

Error occurred prerendering page "/blog/locus-of-control". Read more: https://nextjs.org/docs/messages/prerender-error
--
22:59:28.486 | FetchError: request to http://localhost:3000/api/views/locus-of-control failed, reason: connect ECONNREFUSED 127.0.0.1:3000
22:59:28.486 | at ClientRequest.<anonymous> (/vercel/path0/node_modules/node-fetch/lib/index.js:1461:11)
22:59:28.486 | at ClientRequest.emit (node:events:520:28)
22:59:28.486 | at Socket.socketErrorListener (node:_http_client:442:9)
22:59:28.487 | at Socket.emit (node:events:520:28)
22:59:28.487 | at emitErrorNT (node:internal/streams/destroy:157:8)
22:59:28.487 | at emitErrorCloseNT (node:internal/streams/destroy:122:3)

I identified that the problem stems from /blogs/slugs.js:

const views = await fetch(
    `${process.env.NEXT_PUBLIC_BASE_URL}/api/views/${params.slug}`,
  )
    .then((res) => res.json())
    .then((json) => json.views)

  return {
    props: {
      metadata: article,
      source: mdxSource,
      views: views,
    },
    revalidate: 30,
  }

Only by temporarily disabling the fetch call would I be able to deploy it successfully. Did some Googling and found the following posts:

  1. https://github.com/vercel/next.js/discussions/35779
  2. https://stackoverflow.com/questions/68153761/nextjs-api-app-works-in-deploy-and-development-mode-but-it-doesnt-when-i-run
  3. https://github.com/vercel/next.js/discussions/11210#discussioncomment-1693

Any idea on how to solve this?