Acabo de aprender Next.js y tengo un problema con getInitialProps en _app.jsx . No sé por qué tiene un error de bucle infinito. Por favor, dime por qué bucle infinito y cómo solucionarlo.
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;El ciclo infinito ocurre porque la redirección también ocurre cuando estás en la página de inicio de /login . Para evitarlo, asegúrese de que no se redirija cuando esté en esa página.
MyApp.getInitialProps = async ({ ctx }) => { if (ctx.res && ctx.asPath !== "/login") { ctx.res.writeHead(302, { Location: "/login", }); ctx.res.end(); } return {}; };