Error : Only absolute URLs are supported in nextjs

Viewed 11817

can you please help me to resolve a error Only absolute URLs are supported in nextjs .I am trying to fetch data from server

export async function getStaticProps({
  params,
  preview = false,
  previewData = {}
}) {
  console.log("-----");
  const res = await fetch("/api/basecss/");
  const stylesheet = await res.text(); // Converts response data to text
  return {
    revalidate: 200,
    props: {
      stylesheet
    }
  };
}

here is my code https://codesandbox.io/s/naughty-platform-1xket?file=/pages/index.js:332-639

I am getting this error TypeError: Only absolute URLs are supported

1 Answers

The string you're passing into fetch on line 23 is a relative URL (i.e. it's missing the protocol and domain name, which might be http://localhost:3000/api/basecss or similar)

You need to reference the API endpoint as an absolute URL, including that information. This post has some information on how you can do so

Next.js - Error: only absolute urls are supported

Related