Next.js Discord

Discord Forum

unstable_cache refuses to revalidate

Answered
Cuban Crocodile posted this in #help-forum
Open in Discord
Cuban CrocodileOP
I building a blog on all the latest and greatest NextJs features. Inspired by @Multiflora rose seed chalcid I wanted to add a view count to my posts so that I could see track my massive following 😉

I've built a little service on CloudFlare Workers that tracks views by pathname. This is called from next wrapped in unstable_cache here:
https://github.com/dpeek/website/blob/main/app/stats.ts

The I've got a server component that calls that:
https://github.com/dpeek/website/blob/main/app/views.tsx

And a client component that renders the view count for the current pathname:
https://github.com/dpeek/website/blob/main/app/counter.tsx

For some reason my view counts only revalidate the first page render after a new build 😦
Answered by Cuban Crocodile
Forgot that fetch caches by default server side, so need to set revalidate on that (and then don't need to use ustable_cache at all I think?)
View full answer

17 Replies

Where are you hosting it?
@aardani Where are you hosting it?
Does that matter doe? Like just curious cause won't all features work regardless of VPN or vercel?
@aardani Where are you hosting it?
Cuban CrocodileOP
Hosted on Vercel over here https://dpeek.com
Hmm have you tried it in a reproducible reproduction repo
Also why are you using top level "use server" in a server component?
@Cuban Crocodile
Cuban CrocodileOP
urm I'm not sure, I was debugging and tried a lot of different things 😛
Been using Lee's blog as a reference: https://github.com/leerob/leerob.io
Cuban CrocodileOP
I think I've got it. Discord Duck debugging ftw
Cuban CrocodileOP
Forgot that fetch caches by default server side, so need to set revalidate on that (and then don't need to use ustable_cache at all I think?)
Answer
Cuban CrocodileOP
https://github.com/dpeek/website/blob/main/app/stats.ts I think that fixes it, but something else is broken 😛
marking as resolved for now
@Cuban Crocodile urm I'm not sure, I was debugging and tried a lot of different things 😛
ya but pls dont "use server" in server component where it isnt needed
its not what its used for
looks good, since you can just fetch to external api, then yes you dont need unstable_cache
Cuban CrocodileOP
If you happened to import that component into a client component tree you would need it though, right?
@Cuban Crocodile If you happened to import that component into a client component tree you would need it though, right?
Barbary Lion
You can't import a server component into a client component!