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

284
Visualizações
How to rewrite or change the slug using getStaticProps for only one slug using Next js

I am working on my Next JS project. I have all my slugs which I map through and create all pages using the catch-all route from Next. However, I have a page with slug /home I would like to rewrite this path to the root path like so "/" How can I change the slug inside the getStaticProps? Currently I have to go to /home to see my homepage but I need this to be the absolute root path.

Code:

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 à 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