Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

351
Views
Agregar símbolo (por ejemplo, @) delante de slug en la ruta NextJS

Estoy tratando de agregar @ delante de una URL de slug. Por ejemplo: https://example.com/@username .

Probé @[username].js pero eso no funciona.

¿Es esto posible?

over 4 years ago · Santiago Trujillo
2 answers
Answer question

0

Intente usar reescrituras y algunas expresiones regulares. Next.js usa path-to-regexp bajo el capó.

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

En el lado del cliente, use next/link :

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

0

En su backend, cuando maneje usuarios, coloque la @ en los nombres de usuario de los usuarios. Luego, simplemente trate los nombres de usuario como si fueran slugs de texto sin formato. Esto funciona porque @ está permitido en las URL, al igual que a , b o c .

Ejemplo de trabajo en 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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!