Dev.to WebDev 🛠 Dev 👁 0 📖 1 min read

Next.js ~Handling Expected Errors In Server Components~

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 d

Cover image for Next.js ~Handling Expected Errors In Server Components~
Ogasawara Kakeru
Ogasawara Kakeru

Posted on Nov 17

Next.js ~Handling Expected Errors In Server Components~

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
DEV Community

We're a place where coders share, stay up-to-date and grow their careers.

📰 Read the original article on Dev.to WebDev

Originally published by Dev.to WebDev. Aggregated on AIWithGhost for educational purposes — full credit and traffic to the original publisher.