Next.js Discord

Discord Forum

Real-time data fetch

Answered
Erenay posted this in #help-forum
Open in Discord
Avatar
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)
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;`
View full answer

13 Replies

Avatar
@B33fb0n3 what do you want to archive? Why do you want "real time"?
Avatar
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
Avatar
@Erenay 14.2.18
Avatar
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.
Avatar
@Erenay Isn't there a shorter way to set this in the config file etc.?
Avatar
Upgrade to nextjs15 and it's solved as well. In next15 the GET routes are not cached by default 🤷‍♂️
Avatar
@B33fb0n3 Upgrade to nextjs15 and it's solved as well. In next15 the GET routes are not cached by default 🤷‍♂️
Avatar
next.js gives error even though I useSearchParams in v15. that's why I use v14 unfortunately.
Avatar
@Erenay next.js gives error even though I useSearchParams in v15. that's why I use v14 unfortunately.
Avatar
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
Avatar
@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
Avatar
i solved the problem. i added this at the beginning of all route files.

export const dynamic = 'force-dynamic';
export const revalidate = 0;`
Answer
Avatar
@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;`
Avatar
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