admin 管理员组

文章数量: 1086019

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 =/pages/index.js:332-639

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

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

Share Improve this question asked Jan 13, 2021 at 1:18 user944513user944513 12.8k52 gold badges185 silver badges348 bronze badges 2
  • 1 This: Next.js - Error: only absolute urls are supported and this issue should answer your question. The error is ing 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 Commented Jan 13, 2021 at 1:24
  • 1 @ZacAnger Thanks for answering .but issue is how i use getStaticProps in build time. – user944513 Commented Jan 13, 2021 at 1:36
Add a ment  | 

1 Answer 1

Reset to default 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

本文标签: javascriptErrorOnly absolute URLs are supported in nextjsStack Overflow