Home > Software design >  Revalidate doesn't work on Vercel using NextJS
Revalidate doesn't work on Vercel using NextJS

Time:10-24

I used the provided example from Vercel documentation, to fetch the data from MongoDB after every 15 seconds, but unfortunately the function doesn't work. What should I do to make it work as expected?

export async function getStaticProps() {
  const allData = getSortedData();

  const client = await clientPromise;
  const isConnected = await client.isConnected();
  const alerts = await client.db()
    .collection("alerts")
    .find({})
    .limit(6)
    .toArray();
  const alertsData = JSON.parse(JSON.stringify(alerts));

  return {
    props: {
      allData,
      isConnected,
      alertsData
    },
    revalidate: 15,
  };
}

CodePudding user response:

So revalidate doesn't just fetch new data every 15 seconds. It generates the page at build time, serves it as static content from cache and then waits for the next user to trigger a new build. The first time the new user triggers a build he/she will be shown a stale page. Then in the background the new page will be generated and served to the next user refreshing the certain webpage.

Here is a quick video with timestamp from Lee Robinson explaining it. https://youtu.be/nrfuN_Hyd3Y?t=112

I hope this makes things more clear for you!

CodePudding user response:

useSWR to revalidate data. Here is link to docs: SWR documentation

  • Related