0

I'm trying to pre render page using getServerSideProps in Next.js and everything works perfectly. But what if the axios call failed due to server issue or Network error? How can I repeat the call?

Here's my code:

export async function getServerSideProps() {
  let ImagesList = {};

  await axios
    .get("https://www.*****.com/api/home")
    .then((response) => {
      if (response.data) {
        ImagesList = response.data

      }
    })
    .catch((err) => { });

  return {
    props: {
      ImagesList,
    }
  }
}
Firas SCMP
  • 461
  • 4
  • 18
  • you can do so in the catch ... conditionally (i.e. only when appropriate) `return getServerSideProps()` – Jaromanda X Aug 04 '22 at 12:00
  • Does this answer your question: [How to retry 5xx requests using axios](https://stackoverflow.com/questions/56074531/how-to-retry-5xx-requests-using-axios)? – juliomalves Aug 06 '22 at 12:08

1 Answers1

0

you can try to wrap your axios call inside a while loop,

let result = false;

while(!result) {
await axios
    .get("https://www.*****.com/api/home")
    .then((response) => {
      result = true
      if (response.data) {
        ImagesList = response.data

      }
    })
    .catch((err) => { });
}
Samoox
  • 176
  • 1
  • 2
  • 9