Issues with revalidating data
Unanswered
Himalayan posted this in #help-forum
HimalayanOP
When i update the database it revalidates perfectly in dev mode but when i deploy the site it only revalidates when i redeploy the entire thing. I have included the next revalidate option too so i don't see why it doesn't work. I have tried cache: "no-cache" too btw
12 Replies
American Crow
GET Endpoints in the Route Handler are cached by default.
https://nextjs.org/docs/app/building-your-application/routing/route-handlers#behavior
Also it looks like you are fetching your own Route Handler from a Client Component, which can make sense if the route handler is a webook for example.
In case your endpoint is just getting some catalog data from a database, you'd be better off to scratch that endpoint and do the
https://nextjs.org/docs/app/building-your-application/routing/route-handlers#behavior
Also it looks like you are fetching your own Route Handler from a Client Component, which can make sense if the route handler is a webook for example.
In case your endpoint is just getting some catalog data from a database, you'd be better off to scratch that endpoint and do the
await db.get.catalogitemsin a server component directly and pass the data down to your client component@American Crow GET Endpoints in the Route Handler are cached by default.
https://nextjs.org/docs/app/building-your-application/routing/route-handlers#behavior
Also it looks like you are fetching your own Route Handler from a Client Component, which can make sense if the route handler is a webook for example.
In case your endpoint is just getting some catalog data from a database, you'd be better off to scratch that endpoint and do the `await db.get.catalogitems`in a server component directly and pass the data down to your client component
HimalayanOP
Yeah i have been thinking about the server component but i wanna make it work and not avoid it. So you are saying i should make it a POST request instead? Here is the route code btw
American Crow
No don't make it a POST (That would solve the problem) However it's a GET logically so it can remain being a GET.
You could either
or
You could either
export async function GET() {
headers() // this will opt out of default caching
...
}or
export const dynamic = 'force-dynamic'
export async function GET() {
...
}But yea overall this mongoose call should be converted into a server component. You are doing an extra round trip at the moment
@American Crow No don't make it a POST (That would solve the problem) However it's a GET logically so it can remain being a GET.
You could either
export async function GET() {
headers() // this will opt out of default caching
...
}
or
tsx
export const dynamic = 'force-dynamic'
export async function GET() {
...
}
HimalayanOP
Thank you, I will see if this solves the issue. Is there a documentation on the headers() and export const dynamic?
American Crow
the link i posted
HimalayanOP
Oh my bad didn't see
so whats the difference between
and
?
next: { revalidate: 10 }and
export const dynamic = 'force-dynamic' ?
American Crow
Good question. From my understanding all the options for revlidation in the
https://nextjs.org/docs/app/building-your-application/caching#data-cache. The caching of the GET endpoints is a different cache entirely.
In theory you can opt out of caching in your endpoint and still get stale data when you
I could be wrong though i am not a caching expert
fetch options are concerning the data cachehttps://nextjs.org/docs/app/building-your-application/caching#data-cache. The caching of the GET endpoints is a different cache entirely.
In theory you can opt out of caching in your endpoint and still get stale data when you
fetch it with await fetch(`/api/...`, { cache: 'force-cache' }) for example and vice versa. The other way arround would be your example from this thread. (You opted out of the data cache using no-store or revalidate: 10, however the endpoint was still cached.I could be wrong though i am not a caching expert
@Himalayan so whats the difference between
js
next: { revalidate: 10 }
and
js
export const dynamic = 'force-dynamic'
?
One is a fetch specific, the other is page specific
Priority is given to the fetch specific one, if nothing is present in the fetch call, the dynamic one is used