0

I have a question about a project deployment with NextJS/Vercel.

According to documentation, await fetch is supporting only absolute URLs. Example:

export async function getStaticProps(context) {
  const route = process.env.APIpath + 'api/category/getCategories';
  const res = await fetch(route)
  const json = await res.json()
  return {
    props: {
      data: json,
      },
  };
}

where APIpath: 'http://localhost:3000/',

Question: How can I deploy a project on vercel.com/test/my_project? because when I only change process.env.APIpath to vercel.com/test/my_project = error.

P.S. Error message Build error occurred Error: Export encountered errors on following paths: categories/database/categoryList - the page I am calling getStaticProps(context) above

ChilTest
  • 461
  • 5
  • 18
  • 1
    How about your error message? – Z. Zlatev May 28 '21 at 12:38
  • You cannot fetch your own API routes during static build. See https://github.com/vercel/next.js/discussions/16068 – Eric Burel May 28 '21 at 12:55
  • Added error to question body – ChilTest May 28 '21 at 12:57
  • 1
    you shouldn't fetch your own api routes from the server which is why this is happening. It's supposed to be used from the client What you can do, however, is make async functions in the lib directory for example that rival your api route functions Partition your asynchronous logic between several environments – Andrew Ross May 29 '21 at 04:58
  • Does this help answer your question: [Internal API fetch with getServerSideProps? (Next.js)](https://stackoverflow.com/a/65760948/1870780)? It mentions `getServerSideProps` but the same applies to `getStaticProps`. – juliomalves May 29 '21 at 17:05
  • I can't use fetch as it needs absolute path, trying to do with useSWR – ChilTest Jun 02 '21 at 15:03

0 Answers0