Next.js Discord

Discord Forum

Prevent cache MISS when using Vercel edge caching with stale-while-revalidate

Unanswered
Siamese Crocodile posted this in #help-forum
Open in Discord
Siamese CrocodileOP
Hey,

I am using edge caching of Vercel.
My goal is to:
- Revalidate the server response in the background every minute
- Serve at least a stale version of the response for all visits (except for the very first visit) -> a cache MISS should only happen on the very first page visit

This is my current Cache-Control header:
public, max-age=60, stale-while-revalidate

With this header I still get a cache MISS way to many times. It kinda works (I get cache HIT/STALE responses) but for example when visiting a page that I already visited 1 hour before I still get a MISS. I want the user to get the stale responses for way longer and revalidate in the background.

Any idea how to achieve this?

Thanks for your help!

3 Replies

Singapura
hello. i'm also going through the same issue. they've revamped the caching docs since i last looked to allow CDN-Cache-Control and Vercel-CDN-Cache-Control. i'm experiencing the same behavior with Vercel-CDN-Cache-Control.

my header is the following:
Vercel-CDN-Cache-Control: public, s-max-age=10, stale-while-revalidate=2592000

i want to cache the data at the CDN for 10 seconds, then keep it stale for up to 30 days.
in the limits section, it looks like the cache times are a "best effort" especially for less frequently requested items.
https://vercel.com/docs/edge-network/caching#limits
i've been running a check via checkly to my endpoint in 5 different regions every 10 minutes to check for the performance.

when i run a burst of checks manually in quick succession, the response returns either a HIT or STALE, meaning the cache is hit.

when i leave it to its schedule, i will sometimes get a MISS meaning it was ejected from the cache. while this is definitely not what i expected given my header's value, this is in line with what the docs say.