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

353
Visualizações
Adding symbol (e.g. @) in front of slug in NextJS route

I am trying to add @ in front of a slug URL. For example: https://example.com/@username.

I tried @[username].js but that is not working.

Is this even possible?

over 4 years ago · Santiago Trujillo
2 Respostas
Responde à pergunta

0

Try using rewrites and some regex. Next.js uses path-to-regexp under the hood.

  async rewrites() {
    return [
      {
        source: '/:userId(@[a-zA-Z0-9]+)/:id*',
        destination: "/user/:userId/:id*",
      }
    ]
  }

On client side use next/link:

<Link href={`@${userId}`}>
  <a>user</a>
</Link>
over 4 years ago · Santiago Trujillo Relatório

0

In your backend when you handle users, put the @ in the users' usernames. Then, simply treat the usernames as if they were plain text slugs. This works because @ is allowed in URLs, just like a, b or c.

Working example on StackBlitz.

pages/index.js:

import Link from 'next/link'

const username = '@foobar' // hardcoded example username, this will be recieved from your backend

export default function Index() {
    return (
        <Link href={username}>
            <a>{username}</a>
        </Link>
    )
}

pages/[username].js:

import { useRouter } from 'next/router'

export default function Username() {
    const router = useRouter()
    const { username } = router.query

    return <h1>User: {username}</h1>
}
over 4 years ago · Santiago Trujillo Relatório
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