apophis51 / next-js-Portfolio

https://next-js-portfolio-mauve.vercel.app
2 stars 2 forks source link

Scaling issue with Strapi [Solved!!] #115

Closed apophis51 closed 11 months ago

apophis51 commented 11 months ago

in reference to : https://docs.strapi.io/dev-docs/configurations/api

We faced an issue where strapi was capped at the ammount of data we can pull from the api. we could either fix it at the source code by increasing the rate here:

module.exports = {
  rest: {
    defaultLimit: 25,
    maxLimit: 100,
    withCount: true,
  },
};

or we can abide by the strapi rest api implementation and call the api with the higher api limit described here: https://docs.strapi.io/dev-docs/api/rest/sort-pagination#pagination

apophis51 commented 11 months ago

We proceded with the solution of calling the advanced api with higher limits as follows:

ProgrammingBlogs.pagejs fix:

we went from this:

async function getData() {
    // const res = await fetch('https://malcmind-strapi-cms-production.up.railway.app/api/programming-blogs/')
    const res = await fetch('https://malcmind-strapi-cms-production.up.railway.app/api/programming-blogs/')
    // The return value is *not* serialized
    // You can return Date, Map, Set, etc.

    // Recommendation: handle errors
    if (!res.ok) {
      // This will activate the closest `error.js` Error Boundary
      throw new Error('Failed to fetch data')
    }
    return res.json()
  }

to this:

async function getData() {
    // const res = await fetch('https://malcmind-strapi-cms-production.up.railway.app/api/programming-blogs/')
    const res = await fetch('https://malcmind-strapi-cms-production.up.railway.app/api/programming-blogs?pagination[page]=1&pagination[pageSize]=60')
    // The return value is *not* serialized
    // You can return Date, Map, Set, etc.

    // Recommendation: handle errors
    if (!res.ok) {
      // This will activate the closest `error.js` Error Boundary
      throw new Error('Failed to fetch data')
    }
    return res.json()
  }

BlogControlFix:

we went from this:

async function generateStaticParams(params) {
  let res = await fetch(`https://malcmind-strapi-cms-production.up.railway.app/api/programming-blogs/`)
  let post = await res.json()
  let blogID = ''
  for (let x of post.data){
    if (x.attributes.Title.toLowerCase().split(' ').join('-').includes(params.id)){
      blogID = x.id
    }
  }
   res = await fetch(`https://malcmind-strapi-cms-production.up.railway.app/api/programming-blogs/${blogID}`)
    post = await res.json()
  return post
}

to this:

async function generateStaticParams(params) {
  let res = await fetch(`https://malcmind-strapi-cms-production.up.railway.app/api/programming-blogs?pagination[page]=1&pagination[pageSize]=60`)
  let post = await res.json()
  let blogID = ''
  for (let x of post.data){
    if (x.attributes.Title.toLowerCase().split(' ').join('-').includes(params.id)){
      blogID = x.id
    }
  }
   res = await fetch(`https://malcmind-strapi-cms-production.up.railway.app/api/programming-blogs/${blogID}`)
    post = await res.json()
  return post
}