-2

my next js websites depends on the next js apis which fetch data from a local json file. I'm using getStaticPaths to pre-generate pages. The problem is that it only supports the absolute path which works fine in development mode with "http://lockhost:3000" but its throwing errors when i deploy on vercel.This is the vercel deployment error This is the function

This the the host name conditional

Now how do i fix this error and get the absolute deployment url? i tested the api with local host its send the data without any problem

juliomalves
  • 42,130
  • 20
  • 150
  • 146
  • Please try to include errors and code as text instead of images. – Camilo Sep 06 '22 at 00:42
  • can you try with axios and manually enter the path to make sure it is correct. – Yilmaz Sep 06 '22 at 03:33
  • everthing was correct the thing which was wrong was that i was calling internal apis in SSR which is not supported. Thats why with local host the website was working perfetly fine but during built it was failing cause of internal apis invoking inside SSR – Spicier Ewe Sep 06 '22 at 05:10

1 Answers1

2

Unfortunately, you can not call internal API calls (only external) in getStaticProps/getServerSideProps/

From Next.js getStaticProps documentation:

As getStaticProps runs only on the server-side, it will never run on the client-side. It won’t even be included in the JS bundle for the browser, so you can write direct database queries without them being sent to browsers.

This means that instead of fetching an API route from getStaticProps (that itself fetches data from an external source), you can write the server-side code directly in getStaticProps.

Take the following example. An API route is used to fetch some data from a CMS. That API route is then called directly from getStaticProps. This produces an additional call, reducing performance. Instead, the logic for fetching the data from the CMS can be shared by using a lib/ directory. Then it can be shared with getStaticProps.

brothy
  • 171
  • 6