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,
};
}
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!
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With