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

130
Visualizações
How to use getStaticPaths dynamically in next js?

I have 3 blog categories which I need to paginate static pages for each of them . The problem is the number of pages may vary and I can't get access to anything on what category needs to be fetched in getStaticPaths .

The project folder structure is like this : enter image description here

My code is like this :


export const getStaticPaths: GetStaticPaths = async (props) => {
  
    // const { category } = props.params;  // Cant Access to dynamic category name from the url

    const blogs = await client.getEntries({
      content_type: "blog",
     // "fields.category": category, // Cant Access to dynamic category name from the url
    });

    let pages;
    const howmany = blogs.total / 12;
    pages = Math.ceil(howmany / 1) * 1;
    let paths = [];
    for (var i = 0; i < pages; i++) {
      paths.push({
        params: { page: `${i + 1}` },
      });
    }

  return {
    paths ,
    fallback: "blocking",
  };
};

export const getStaticProps: GetStaticProps = async ({ params }) => {
  const { category, page } = params;

  const limit = 2;
  let skip;
  if (JSON.parse(page) === 1) {
    skip = 0;
  } else {
    skip = JSON.parse(page) * limit - limit;
  }
  const blogs = await client.getEntries({
    content_type: "blog",
    limit: limit,
    skip: skip,
    "fields.category": category,
  });

  return {
    props: {
      page: JSON.parse(page),
      blogs: blogs.items,
    },
    revalidate: 1,
  };
};

I need to get access to dynamic category string from url in getStaticPaths so I can get the exact number of blogposts for that specific category but couldn't have access to anything at all .

How can I fix this problem ? Is there a workaround to fix this ?

Thanks in advance .

about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

I solved the issue with the help of Ivan atias Christian Hagelid and generated all categories pages in getStaticPaths and returned the final path like this :

 let paths = [];

  newCategories.forEach((eachCategory: any) => {
    let pages;
    const howmany = eachCategory.posts.length / limit;
    pages = Math.ceil(howmany / 1) * 1;
    for (var i = 0; i < pages; i++) {
      paths.push({
        params: { page: `${i + 1}`, category: eachCategory.category },
      });
    }
  });

  return {
    paths,
    fallback: false,
  };
about 4 years ago · Juan Pablo Isaza 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