Durable cache on Netlify
By Flavio Copes
Cache Netlify Function responses across regions with the durable CDN directive, max-age, and stale-while-revalidate.
I need to remember this interesting technique so let me write a blog post.
I read this on Jason Lengstorf’s blog.
Netlify’s durable cache adds a shared backing layer to its edge cache. When one region generates a cached response, other regions can reuse it instead of invoking the function again.
Set the durable directive in the Netlify-specific CDN header:
export default async () => {
return new Response('Hello world', {
headers: {
'Netlify-CDN-Cache-Control':
'public, durable, max-age=60, stale-while-revalidate=120'
}
})
}
This example keeps a response fresh for 60 seconds. For the next 120 seconds, the cache may serve the stale response while it revalidates in the background.
Use Netlify-CDN-Cache-Control when the rule is meant for Netlify’s CDN but not the visitor’s browser cache. Choose durations based on how stale the content is allowed to become, and never cache personalized or authenticated responses under a shared key.
The durable directive applies to Netlify Function responses, not Netlify Edge Function responses. Netlify also notes that the cache is eventually consistent, so it does not guarantee exactly one function invocation across every region.
See Netlify’s current caching documentation for cache-key variation and on-demand invalidation.
Related posts about services: