Problem with server component cache
Unanswered
Wuchang bream posted this in #help-forum
Wuchang breamOP
One question. I have form on the another page, that creates object to the database (client component). Then there is a page (server component) that contains list of those objects, that are retrieved from database using sequelize (so no http fetch). Results are cached so I have to refresh page so I see the new objects when Ill go to page via link. Whats the "next"-way to purge cache (or show) the most recent result? Any links to blogs or documentation? I have tried lots of stuff but nothin seems to work as I expected...
16 Replies
use this to revalidate the cache after the mutation
Wuchang breamOP
Thx. I was checking also that but couldn't get it work. I test it again 🙂
@Wuchang bream Thx. I was checking also that but couldn't get it work. I test it again 🙂
oh what is not working? I would be able to help if you can share some code
Wuchang breamOP
Sure! Ill test it later. I might understand something wrong.
There are versions of Next where those functions are buggy
on 14.0, revalidatePath and revalidateTag works ok
revalidatePath is tied to routes so you might want to give a shot about revalidateTag
- wrap your data fetching call with "unstable_cache", and give it a key:
const rscGetData = unstable_cache(callSequelize, [], {tags:["my-data"]})
- then you can invalidate the tag "my-data" => all pages using the same data will be invalidated
const rscGetData = unstable_cache(callSequelize, [], {tags:["my-data"]})
- then you can invalidate the tag "my-data" => all pages using the same data will be invalidated
I am actually releasing a course on this very topic, getting always up-to-date data while keeping the page static thanks to those APIs
It's an unstable cache so... unstable haha
but just so you know both patterns
revalidatePath is fine if you don't have many pages but won't scale to very complex apps
Revalidation is supposed to clear the browser cache too
(remember that dynamic routes are cahed for 30 sec to avoid spurious UX on back navigation https://github.com/vercel/next.js/discussions/54075 but revalidation also revalidates the client cache)
Maybe you are revalidating in an API route, I think only revalidating in a Server Action will also purge the router cache (the client-side one)