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

526
Vistas
Middleware for each route server side in Next.js

I'm trying to figure out a way to have some code run on initial page load for each request. I want to be able to identify the request domain and redirect to a specific part of the website based on it.

From what I can understand I could add getInitialProps to a custom _app page but that would disable all optimization from next. Also _app doesn't seem to support getServerSideProps which I considered as an alternative.

Another way I thought about doing it was to have getServerSideProps in each page of the website with the check I have to do, but this would make every page rerender on server side for each new request, disabling rendering at build time even for pure static pages.

Is there any other solution I can adopt?

EDIT

Basically I need the request object so that I can do something like this:

export const getServerSideProps: GetServerSideProps = async ({
  req
}) => {
  if (req.headers.host === 'something') {
     return {
       redirect: {
         permanent: false,
         destination: "/"
       }
     }
  }
about 4 years ago · Juan Pablo Isaza
2 Respuestas
Responde la pregunta

0

I would put solutions to this in the order below:

  1. do redirect on your server(nginx or something), fastest solution
  2. you could create custom [Next.js server][1] to handle redirect
  3. custom App as you said
  4. RouteGuard used in each page which would redirect
about 4 years ago · Juan Pablo Isaza Denunciar

0

You can use Next.js redirects in your next.config.js file.

module.exports = {
    async redirects() {
        return [
            {
                source: '/(.+)',
                has: [
                    {
                        type: 'header',
                        key: 'host',
                        value: 'something'
                    }
                ],
                permanent: false,
                destination: '/'
            }
        ]
    }
}

This rule will match any path that's not /, and redirect if it contains the desired host header value.

about 4 years ago · Juan Pablo Isaza 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