How do you return a HTTP status code for server rendered components?

282 views Asked by At

I have the following server rendered component in the NextJS 13 experimental app directory:

const Example = async () => {
  const { data, error } = await fetchData()

  return null
}

const fetchData = async () => {
  try {
    const response = await fetch(`${process.env.RESTAPI_URI}/data?populate=*`)
    const json = await response.json()
    return {
      data: json.data,
    }
  } catch (err: any) {
    return {
      data: [],
      error: err?.message,
    }
  }
}

export default Example

I want to make it so that if the fetch fails, the HTTP response code sent to the user is something other than 200. Is this possible?

0

There are 0 answers