Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

599
Views
Next.js, redirect to 404 page inside getServerSideProps

I am looking for a best practice in a server-side rendered page on handling an HTTP 404 if the requested page does not have an underlying server-side resource.

For example, let's assume the page requested is http://localhost:3000/places/5. In my SSG implementation:

export async function getServerSideProps(context) {
  const placeId = context.params.placeId;
  const places = await getPlace(placeId);
  
  if (!places.length) { /* is there anything here I can do to facilitate a 404? this place does not exist in the db */ }

  return {
    props: {
      places[0],
    },
  };
}

Should be self-explanatory but if the requested id, in this case 5 is not a place that's in my DB, how do I handle this as an HTTP 404?

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

You can do that by returning an object like below, with notFound: true. And it would redirect to 404 page if your Next.js version is greater than v10.

The notFound boolean allows the page to return a 404 status and 404 Page. With notFound: true, the page will return a 404 even if there was a successfully generated page before. More here on Next.js's offical documentation.

export async function getServerSideProps(context) {
  const placeId = context.params.placeId;
  const places = await getPlace(placeId);

  if (!places.length) { 
   return {
     notFound: true,
   }
  }

  return {
    props: {
      places[0],
    },
  };
}
about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!