Real-time data fetch
Answered
Erenay posted this in #help-forum
ErenayOP
Whenever I make a request to the API, I get the data from when I built my application. how do I get real-time data?
(translator used)
(translator used)
Answered by Erenay
i solved the problem. i added this at the beginning of all route files.
export const dynamic = 'force-dynamic';
export const revalidate = 0;`
13 Replies
@Erenay Whenever I make a request to the API, I get the data from when I built my application. how do I get real-time data?
(translator used)
what do you want to archive? Why do you want "real time"?
@B33fb0n3 what do you want to archive? Why do you want "real time"?
ErenayOP
I make a POST request to the API and create a data in the database and then when I try to retrieve the data with a GET request, I get the data of the application when it was built (ie null because there is no such data). but it seems to be created in the database.
which one should I use
@Erenay I make a POST request to the API and create a data in the database and then when I try to retrieve the data with a GET request, I get the data of the application when it was built (ie null because there is no such data). but it seems to be created in the database.
which nextjs version are you using?
@Erenay 14.2.18
then your GET route handlers are cached by default. You need to revalidate them after you made a change. You can revalidate by path like:
You call that function after you inserted your data.
revalidatePath("/api/whatever/route")
You call that function after you inserted your data.
@B33fb0n3 then your GET route handlers are cached by default. You need to revalidate them after you made a change. You can revalidate by path like:
revalidatePath("/api/whatever/route")
You call that function after you inserted your data.
ErenayOP
Isn't there a shorter way to set this in the config file etc.?
@Erenay Isn't there a shorter way to set this in the config file etc.?
Upgrade to nextjs15 and it's solved as well. In next15 the GET routes are not cached by default 🤷♂️
@B33fb0n3 Upgrade to nextjs15 and it's solved as well. In next15 the GET routes are not cached by default 🤷♂️
ErenayOP
next.js gives error even though I useSearchParams in v15. that's why I use v14 unfortunately.
@Erenay next.js gives error even though I useSearchParams in v15. that's why I use v14 unfortunately.
yea, I thought about you get other errors when you upgrade to v15. So in my opinion I would add the
revalidatePath
to the function that POST the data@B33fb0n3 yea, I thought about you get other errors when you upgrade to v15. So in my opinion I would add the revalidatePath to the function that POST the data
ErenayOP
i solved the problem. i added this at the beginning of all route files.
export const dynamic = 'force-dynamic';
export const revalidate = 0;`
Answer
@Erenay i solved the problem. i added this at the beginning of all route files.
js
export const dynamic = 'force-dynamic';
export const revalidate = 0;`
keep in mind, that none of your data is then cached and your usage will go to the moon if you use it to much