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

276
Vistas
Cómo reescribir o cambiar el slug usando getStaticProps para solo un slug usando Next js

Estoy trabajando en mi proyecto Next JS. Tengo todos mis slugs que mapeo y creo todas las páginas usando la ruta catch-all de Next. Sin embargo, tengo una página con slug /home . Me gustaría reescribir esta ruta a la ruta raíz como " / " ¿Cómo puedo cambiar la slug dentro de getStaticProps ? Actualmente tengo que ir a /home para ver mi página de inicio, pero necesito que esta sea la ruta raíz absoluta.

Código:

 import { getClient, usePreviewSubscription } from "../lib/sanity"; import { groq } from "next-sanity"; import { getQueryFromSlug } from "../lib/helpers"; import dynamic from "next/dynamic"; const PageSingle = dynamic(() => import("../components/layouts/PageSingle")); const NewsSingle = dynamic(() => import("../components/layouts/NewsSingle")); interface Props { data: any; preview: boolean; } export default function Page({ data, preview }: Props) { const { data: pageData } = usePreviewSubscription(data?.query, { params: data?.queryParams ?? {}, initialData: data?.pageData, enabled: preview, }); const { docType } = data; return ( <> {docType === "home" && <PageSingle page={pageData} />} {docType === "page" && <PageSingle page={pageData} />} {docType === "article" && <NewsSingle post={pageData} />} {docType === "case" && <NewsSingle post={pageData} />} </> ); } export async function getStaticPaths() { const pageQueries = await getClient().fetch( groq`*[_type in ["homePage", "page", "article", "case"] && defined(slug.current)][].slug.current` ); // Split the slug strings to arrays (as required by Next.js) const paths = pageQueries.map((slug: string) => ({ params: { slug: slug.split("/").filter((p) => p) }, })); return { paths, fallback: false }; } export async function getStaticProps({ params }: any) { // Every website has a bunch of global content that every page needs, too! // const globalSettings = await client.fetch(globalSettingsQuery); // A helper function to work out what query we should run based on this slug const { query, queryParams, docType } = getQueryFromSlug(params.slug); // Get the initial data for this page, using the correct query const pageData = await getClient().fetch(query, queryParams); return { props: { data: { query, queryParams, docType, pageData }, }, }; }
about 4 years ago · Juan Pablo Isaza
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