Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

534
Vistas
Infinite loop when redirecting in getInitialProps in Next.js

I have just learn Next.js and I have one problem with getInitialProps in _app.jsx. I don't know why have an infinite loop error. Please tell me why infinite loop and how to fix it.

const MyApp = ({ Component, pageProps }) => {

  return (
    <>
       <Component {...pageProps} />
    </>
  );
};

MyApp.getInitialProps = async ({ ctx }) => {
  console.log("run 1");
  if (ctx.res) {
    console.log("run here");
    ctx.res.writeHead(302, {
      Location: "/login",
    });
    ctx.res.end();
  }
  return {};
};

export default MyApp;
over 4 years ago · Santiago Trujillo
1 Respuestas
Responde la pregunta

0

The infinite loop happens because the redirect also happens when you're on the /login page. To prevent it, make sure no redirection happens when you're on that page.

MyApp.getInitialProps = async ({ ctx }) => {
    if (ctx.res && ctx.asPath !== "/login") {
        ctx.res.writeHead(302, {
            Location: "/login",
        });
        ctx.res.end();
     }
     return {};
};
over 4 years ago · Santiago Trujillo Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda