Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

183
Visualizações
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 Respostas
Responde à pergunta

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda