3

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

user944513
  • 12,247
  • 49
  • 168
  • 318
  • 1
    This: [Next.js - Error: only absolute urls are supported](https://stackoverflow.com/questions/44342226/next-js-error-only-absolute-urls-are-supported) and [this issue](https://github.com/node-fetch/node-fetch/issues/481#issuecomment-403595704) should answer your question. The error is coming from `fetch`, and it tells you what the problem is: relative URLs aren't supported. You need a whole URL, not just a path. – Zac Anger Jan 13 '21 at 01:24
  • 1
    @ZacAnger Thanks for answering .but issue is how i use `getStaticProps` in build time. – user944513 Jan 13 '21 at 01:36

1 Answers1

3

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