Updated Data is not fetching until i rebuild again
Answered
Thrianta posted this in #help-forum
ThriantaOP
I'm working on a project with Next.js, and for the database, I'm using MongoDB. Everything works fine when I'm working on localhost:3000. However, when I hosted my site, I encountered an issue. When I try to post anything, it's stored in the database instantly, but I can't view the latest data until I rebuild my website in Vercel .how to solve this issue
Answered by Barbary Lion
you must revalidate the data
eather at each request, using
export const dynamic = ‘force-dynamic’ at the top of your page.tsx code where the data is fetched
this will query your database for each person loading your page
or you can revalidate at given time intervals, this will query your database when a user query your page after the data has been invalidated (after a certain time),
to use this, as far as i know you have to use unstable_cache
eather at each request, using
export const dynamic = ‘force-dynamic’ at the top of your page.tsx code where the data is fetched
this will query your database for each person loading your page
or you can revalidate at given time intervals, this will query your database when a user query your page after the data has been invalidated (after a certain time),
to use this, as far as i know you have to use unstable_cache
3 Replies
Barbary Lion
you must revalidate the data
eather at each request, using
export const dynamic = ‘force-dynamic’ at the top of your page.tsx code where the data is fetched
this will query your database for each person loading your page
or you can revalidate at given time intervals, this will query your database when a user query your page after the data has been invalidated (after a certain time),
to use this, as far as i know you have to use unstable_cache
eather at each request, using
export const dynamic = ‘force-dynamic’ at the top of your page.tsx code where the data is fetched
this will query your database for each person loading your page
or you can revalidate at given time intervals, this will query your database when a user query your page after the data has been invalidated (after a certain time),
to use this, as far as i know you have to use unstable_cache
Answer
Barbary Lion
this answer is if you are using app router
you can also revalidate page when data is written on your database, using on demand revalidation and revalidateTag