Menu

Next.js ~Handling Expected Errors In Server Components~
📰

Next.js ~Handling Expected Errors In Server Components~

DEV Community: nextjs·Ogasawara Kakeru·1 day ago
#wx17H2vD
#devtonextjs#devto#dev#error#const
Reading 0:00
15s threshold
Cover image for Next.js ~Handling Expected Errors In Server Components~

Ogasawara Kakeru

When fetching data inside of a Server Component, you can use the response to conditionally render an error message or redirect.

export default async function Page() {
  const res = await fetch(`https://...`)
  const data = await res.json()
  if (!res.ok) {
    return 'There was an error.'
  }
  return '...'
}

Enter fullscreen mode Exit fullscreen mode