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

275
Views
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
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!