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

128
Vistas
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 Respuestas
Responde la pregunta

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