How to get the absolute url while deploying next js website on vercel?

Viewed 44

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

1 Answers

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.

Related