Next.js Discord

Discord Forum

next.revalidate behaviour

Answered
borghese posted this in #help-forum
Open in Discord
Hey, I was wondering how the revalidate option on fetch requests actually works, i'm trying to implement it in a simple page and looks like it has no effect on data fetched from the api, I used it with export const revalidate = 30; on top of the page and i'm fetching the local Next api with
  const { inServizio } = await (await fetch(`${process.env.NEXT_PUBLIC_LOCAL_API_URL}/api/cartellino/servizio/${session.user.id}`)).json()

but nothing actually changes on the client 🤔, maybe I should use react's useState to show updated content?
Answered by riský
if on client, you should be able to be relative (/api) and also make sure that route has revalidation of 0 (or whatever you wanted)
View full answer

14 Replies

basicly fetch also has a cache by default (in future versions it will be not default), so also add [export const fetchCache = "default-no-store"](https://nextjs.org/docs/app/api-reference/file-conventions/route-segment-config#fetchcache) or change the fetch cache options in the actual fetch req function
yeah it should, as the page level cache should exist and fetch should also stop caching
@riský yeah it should, as the page level cache should exist and fetch should also stop caching
looks like it's not revalidating actually, data stays the same even after some minutes. does revalidation affects data that is passed as prop to a client component?
ok maybe fetch with revalidate: 0 is better?
@borghese looks like it's not revalidating actually, data stays the same even after some minutes. does revalidation affects data that is passed as prop to a client component?
but revalidate is kinda sus, it still shows stale data (just refreshes it in background for next visit to be accurate)
the cause here is that OP is fetching from a local route handler which is static by default, so although the fetch is made every x seconds, they all return the same build-time value. @OP: [Don't do that](https://nextjs-faq.com/fetch-api-in-rsc)
ahhhhh maybe 3 levels of caching :(
yeah mb i didnt see the localfetch part
@joulev the cause here is that OP is fetching from a local route handler which is static by default, so although the fetch is made every x seconds, they all return the same build-time value. @OP: [Don't do that](<https://nextjs-faq.com/fetch-api-in-rsc>)
okay got it, thanks a lot.
i updated the fetch with an async function that queries the db (if it's the correct way).
all of this was on the server side, which approach should I use to get updated data on the client?
should i query the local api because i'm inside a client component or just import the same function that queries the db?
i'm asking this because in the client component I'm trying to periodically update the data displayed, the update occurs once every 60 seconds inside a setTimeout that queries the local api to then update the data with react's setState.
This looks like it's working but it creates a ton of promises on the client, i can see that every 60 seconds it doesn't send just 3 requests, it sends like 10~20 of them
if on client, you should be able to be relative (/api) and also make sure that route has revalidation of 0 (or whatever you wanted)
Answer
just make sure to put that in a useEffect
Got it, thanks a lot 👍