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

127
Views
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 answers
Answer question

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 Report
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!