Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Revalidate doesn't work on Vercel using NextJS [duplicate]

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,
  };
}
like image 518
user3630024 Avatar asked Jul 03 '26 17:07

user3630024


1 Answers

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!

like image 145
Wesley Janse Avatar answered Jul 05 '26 07:07

Wesley Janse



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!