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

181
Views
How to show error page without changing current URL

I have a custom 500 error page called 500.tsx and I want to able to show this page when request from client fails with status of 500, but keeping client on old url (for example /auth/register). I have a axios listener on error which should do all the work. But could not find a way to do this using next/router. Any help appreciated

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

0

Didn't realize OP wanted to show 500 page at first.

You could look into router.isFallback but I am not entirely sure if that's appropriate for this. It's supposed to be for Page Loading Indicators, but in your case, since the page will never load, it could work.

https://nextjs.org/docs/basic-features/data-fetching#fallback-true

 return router?.isFallback 
   ? <Custom500Page/>
   : <RegularPage/>

You can set this variable from getStaticPaths method, which runs during build time and is usually used for [slug] pages.

// This function gets called at build time
export async function getStaticPaths() {
  // Fetch paths to your slug pages here

  return {
    // paths,
    fallback: true
  }
}

Showing 404 page is quite straight forward & This is how we do.

Rendering 404 page

You can conditionally export a variable called notFound as true from getStaticProps method.

If the URL is invalid and you don't data to render the page, you simply pass that notFound prop as true and then the default or custom 404 page will show up automatically preserving your invalid url.

// This function gets called at build time
export async function getStaticProps({ params, preview = false }) {
  let pageData

  try {
    // fetch pageData from API here
  } catch (error) {
    // catch errors
  }

  // check validity or emptyness of data
  // invalid URL (slug) -> empty data from API
  // valid URL (slug) -> valid data from API

  return isObjectEmpty(pageData)
    ? { notFound: true }
    : {
        props: {
          pageData
          // pass other props to the page
        }
      }
}

NextJS Docs on getStaticProps & not-found

https://nextjs.org/docs/basic-features/data-fetching#getstaticprops-static-generation

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!