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

152
Visualizações
NextJS Rewrites keeps on refreshing

I've two projects,

  • Project 1 - React + Electrode - www.example.com/au
  • Project 2 - Next Js - www.example.com/au/sell-your-car

Project 2 has a middleware to detect whether the request is coming from desktop/mobile.

The Project 2 page folder looks like the following:

  • pages
    • _viewport
      • desktop
        • au
          • sell-your-car
      • mobile
        • au
          • sell-your-car

_middleware.tsx

function getViewport(userAgent: UserAgent | null | undefined) {
  if (!userAgent?.ua) return;
  const isMobile = /Android|webOS|iPhone|iPad|iPod|BlackBerry|IEMobile|Opera Mini/i.test(userAgent.ua);
  return isMobile ? "mobile" : "desktop";
}

/** @type {import("next/server").NextMiddleware} */

export function middleware(req: NextRequest) {
  const url = req.nextUrl.clone();

  // Prevent internals from being accessed canonically
  // (i.e. if you try to go to example.com/_viewport it should give a 404)
  if (url.pathname.startsWith(`/_viewport`)) {
    return new Response(null, { status: 404 });
  }

  const viewport = getViewport(req.ua);

  url.pathname = `_viewport/${viewport}${url.pathname}`;

  return NextResponse.rewrite(url);
}

I want to set up rewrites in such a way that they should support all the routes after the /sell-your-car route.

e.g.

  • www.example.com/au/sell-your-car
  • www.example.com/au/sell-your-car/1
  • www.example.com/au/sell-your-car/1/2 (and so on)

Other than that all the routes should redirect to the Project 1 (React)

next.config.js

async rewrites() {
    return [
      {
        source: "/:path*",
        destination: "/:path*"
      },
      {
        source: "/:path*",
        destination: "http://localhost:3001/:path*" // Rewrite non-migrated routes to Create React App
      }
    ];
  }

With the current config, the /sell-your-car page is getting refreshed continuously.

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