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

134
Visualizações
Can I access dynamic route name in next.js middleware?

I have a route at /:username.

In my _middleware.ts file I would like do do something like this

export function middleware(req: NextRequest) {
  const isDynamicRoute = req.nextUrl.pathname === "/[username]";
  
  if (isDynamicRoute) {
    return undefined
  }

  // handle other routes...
}

The issue is that I can't seem to access this dynamic route name in the middleware. I see /test as the pathname instead of /[username].

When I log the request I see what I'm looking for, but it is inside a [Symbol(internal request)] key (along with a separate symbol key that includes the real pathname). Is it possible to access this value reliably in the middleware file? Or do I need to handle this logic in _app.ts? (see log below)

  [Symbol(internal request)]: {
    credentials: 'same-origin',
    headers: BaseHeaders [Headers] { [Symbol(map)]: [Object] },
    method: 'GET',
    referrer: 'about:client',
    redirect: 'follow',
    url: NextURL {
      href: '/test',
      origin: '',
      protocol: '',
      username: '',
      password: '',
      host: '',
      hostname: '',
      port: '',
      pathname: '/test',
      search: '',
      searchParams: URLSearchParams {},
      hash: ''
    }
  },
  [Symbol(internal request)]: {
    cookieParser: [Function: cookieParser],
    geo: {},
    ip: undefined,
    page: { name: '/[username]', params: [Object] },
    url: NextURL {
      href: '/test',
      origin: '',
      protocol: '',
      username: '',
      password: '',
      host: '',
      hostname: '',
      port: '',
      pathname: '/test',
      search: '',
      searchParams: URLSearchParams {},
      hash: ''
    }
  }
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