2

The Problem

I have a simple next.js application. On the index site I have following code:

export async function getServerSideProps(ctx: NextPageContext) {
  const json = await myGet("/api/people", ctx);
  return {
    props: {
      people: json,
    }, // will be passed to the page component as props
  };
}

myGet does just a simple fetch (with isomorphic fetch) to an api route with the url /api/people. This however fails with this error:

Server Error TypeError: Only absolute URLs are supported

My expectation would be that I can use a path just like "/api/people" since I serve the app from the same base url. In next.js do I have to explicitly provide the full URL? (e.g. https://my-app/api/people)

Thx guys

GeraltDieSocke
  • 1,524
  • 3
  • 20
  • 51
  • Does https://github.com/node-fetch/node-fetch/issues/481#issuecomment-403595704 or https://stackoverflow.com/questions/44342226/next-js-error-only-absolute-urls-are-supported help? – mjwills Jun 07 '20 at 08:10
  • Interesting, so it's not a next.js problem but a node fetch problem? I mean that solves it, but I was just curious why I can't just use /api/people since I won't have to deal with checking if prod or development. – GeraltDieSocke Jun 07 '20 at 08:12
  • You need the explicit full URL because that request is made from the server, not the client. However, in your case, you shouldn't even be making a request to the API route. Instead, use the logic of the API route directly. See [Internal API fetch with getServerSideProps? (Next.js)](https://stackoverflow.com/questions/65752932/internal-api-fetch-with-getserversideprops-next-js). – juliomalves Nov 13 '22 at 11:22

1 Answers1

0

I got this exact same error while using jest framework to mock the API post request, and the reason for the error was type error when calling the end point in functionality.

Pasindu Perera
  • 489
  • 3
  • 8