netlify/context-and-tools/netlify-caching
Claude Code and agent skill by netlify/context-and-tools
- community
Checking that the SKILL.md file is still in the repository.
Cache dynamic and static responses on Netlify's CDN from Functions, Edge Functions, and proxies. Use when you add caching or cache-control headers to a function response, tune cache TTL or stale-while-revalidate, set up the durable cache, vary a cache key by query/header/cookie/country/language, purge or invalidate the cache by site or cache tag, use the programmatic Cache API (caches.open/match/put) or @netlify/cache helpers (fetchWithCache/cacheHeaders/getCacheStatus), speed up an expensive A…
Install
npx vskill@latest install netlify/context-and-tools/netlify-cachingSKILL.md
Netlify caching
Cache-control header to reach for
Dynamic responses (Functions, Edge Functions, proxies) are NOT cached by default. You must opt in. Set Netlify-CDN-Cache-Control on the response:
Header choice (most specific wins; CDN-Cache-Control/Cache-Control always pass downstream):
Netlify-CDN-Cache-Controlapplies to the Netlify CDN only. Reach for this.CDN-Cache-Controlapplies to all CDNs that support it.Cache-Controlapplies to any CDN or the browser.
Legacy path to avoid: On-demand Builders do not support these headers or Netlify-Vary. They use a TTL pattern and key on URL path only. Don't reach for ODBs in new code.
Footguns (read first)
- Static assets are fresh for up to a year. A shorter
max-ageis ignored. They change only on a new deploy or manual purge. - basic-auth on ANY page disables caching for the ENTIRE site.
- `durable` is serverless-only. It has no effect on Edge Function responses.
- Cache API silently drops responses over 10 MB.
cache.put()doesn't store them and later lookups miss. Compress the content or split it across multiple entries. - Never opt sensitive content out of automatic invalidation. It can stay publicly cached after deploys/firewall changes.
Directives
publiccaches the response.privatecaches it in the browser only, not in Netlify's shared cache.no-storeprevents caching.s-maxage=Nsets seconds in Netlify's shared cache (overridesmax-agethere).max-age=Nsets seconds in any cache.stale-while-revalidate=Nserves stale for N seconds after expiry while revalidating in the background.durable(serverless only) stores the response in Netlify's durable cache so other edge nodes reuse it instead of re-invoking the function.
Defaults when no header is set:
- Static:
Netlify-CDN-Cache-Control: public, s-maxage=31536000, must-revalidate - Dynamic:
Cache-Control: public, max-age=0, must-revalidate
Cache key variation — Netlify-Vary
Comma-delimited instructions on the response; pipe-delimited value lists:
query=a|bvaries on a subset; barequeryvaries on all params. Keys are case-sensitive; param order is irrelevant.header=Device-Type|App-Versionsupports custom and most standard headers.language=en|es+ptuses+for groups. It is checked againstAccept-Languagewith quality weighting.country=us|es+ptuses GeoIP with ISO 3166-1 two-letter codes;+groups.cookie=ab_test|is_logged_intargets specific keys, not the wholeCookieheader.
Cannot vary by header on: Accept*, Cache-Control, Connection, Content-Length, Cookie, Host, If-*, Range, Referer, Upgrade, User-Agent. For language/cookie/format, use Vary: Accept-Language/Vary: Cookie or the specific Netlify-Vary instruction.
Consistency rule: a URL must return the same Netlify-Vary on every response. The first cached response's instructions win and later ones are ignored.
Netlify-Vary and standard Vary are both respected. Use Vary for format/encoding, and to pass instructions to an upstream CDN like Cloudflare.
Cache tags & opt-out
Tag responses for taggable purging:
Netlify-Cache-Tag(Netlify CDN) wins overCache-Tag(passed downstream). Some providers stripCache-Tag, so set both when proxying through them.- Constraints: case-insensitive, UTF-8 only, ≤1024 chars/tag, ≤500 tags/response.
Opt a response out of automatic atomic-deploy invalidation with Netlify-Cache-ID. Values are comma-separated, auto-registered as cache tags for purging, and have a separate 500-ID limit:
After opting out, purge on demand after relevant changes, e.g. redirect/proxy or function changes behind a Netlify-Cache-ID.
On-demand invalidation (purge)
Purge from a deployed function with purgeCache. The site ID is passed automatically:
Purge by tag, optionally targeting a deploy/subdomain:
Lambda-compatible functions use the legacy module.exports.handler = async (event, context) => {…} signature. They must pass context.clientContext.custom.purge_api_token:
Direct API (from outside a function): POST https://api.netlify.com/api/v1/purge with Authorization: Bearer <personal_access_token> and Content-Type: application/json:
- Purge by site: pass
site_idorsite_slug. - Purge by tag: pass
cache_tagsplus the site. - Omitting
cache_tagspurges the whole site. An emptycache_tagslist purges NOTHING. - Identifier mapping: in the UI (Project configuration > General > Project details > Project information), Project ID =
site_idand Project name =site_slug. See https://docs.netlify.com/api-and-cli-guides/api-guides/get-started-with-api#get-site. - Rate limit: each tag or site can be purged only twice per 5s. Exceeding it returns
429.
Cache API (caches global)
Programmatic read/write of HTTP responses from Functions/Edge Functions. Use it to cache individual components of a route or arbitrary fetches, alongside header-based route caching.
Scope rule: call caches.open() anywhere, but call match/put/delete only inside the request handler. Doing them at module/global scope throws.
CacheStorage subset:
caches.match(request)returns aResponsefrom any cache, orundefined.caches.open(name)returns aCache. Distinct names fragment the cache and lower the hit ratio, so use few, meaningful names.
Cache methods (all require caches.open()):
cache.match(request)returns aResponseorundefined.cache.put(request, response)adds a response.cache.add(request)/cache.addAll(requests)fetch and store.cache.delete(request)returnstrue.keys()is not implemented, so there is no way to list contents.
Consistency: reads/writes are strongly consistent. Deletes are eventually consistent, so a deleted entry may still return briefly.
Cannot cache:
- Partial responses (206),
Vary: *, or non-GETmethods. - Responses over 10 MB. These are silently not stored.
Responses also need all of the following, otherwise storage errors:
- a cache-control header with
max-age/s-maxage≥ 1s public(notprivate/no-cache/no-store)- a 2xx status
For responses you don't control, rewrite headers with fetchWithCache.
Limits per invocation: 100 lookups, 20 insertions/deletions. Beyond these, further lookups return nothing and writes/deletes no-op. Limits are shared across edge functions in a request, but separate between serverless and edge functions.
Cache data is per-region (not replicated). It is auto-invalidated on redeploy and on max-age/s-maxage expiry.
@netlify/cache module
Install to get helpers, time constants (MINUTE/HOUR/DAY), and a caches export for local dev:
Local-dev workaround: the caches global isn't part of Node.js. Netlify provides it in its Functions/Edge runtimes, both live and under netlify dev. If you run your framework's own dev server, the global is undefined and throws. Import it instead:
This requires Netlify CLI 20.0.3+. Nothing persists locally: lookups return nothing, and writes/deletes don't mutate. There is no functional change from the global.
cacheHeaders(settings) → header object
For only generic (non-Netlify) headers, use the cdn-cache-control npm module instead.
fetchWithCache(resource, options?, cacheSettings?)
Drop-in fetch that returns a cached response, or fetches, stores, and returns. cacheSettings override conflicting response headers. With swr, background revalidation is handled automatically.
getCacheStatus(response | headers | headerString)
Returns { hit, caches: { durable: { hit, stale, stored, ttl }, edge: { hit, stale } } }.
needsRevalidation(response) → boolean
Only needed when calling cache.match/cache.put directly (not with fetchWithCache + swr). It is true when a Cache-API response is stale within its SWR window. Return the stale response, then revalidate in context.waitUntil and cache.put the fresh copy:
Durable cache
Add durable (serverless only) so edge nodes lacking a local copy check the shared durable cache before invoking the function. This means fewer invocations and better cache-miss latency.
This is the start of the file. Read the full SKILL.md on GitHub.
Security
- Security scanPassed, 100 of 100Pattern checks on the skill's files.
- Model reviewNot runA model review is scheduled for the next rescan.
Quality evaluation
- Task evaluationNot runNo evaluation yet. Run one in Skill Studio.
Source
- SKILL.md in the repositoryCheckingskills/netlify-caching/SKILL.md
Popularity
- Repository stars
- 13
- Repository forks
- 4
- 7-day trend score
- 13
Stars and forks are for the whole GitHub repository, not this skill alone. Refreshed 12 Apr 2026, 6 months ago.
Works with
Any agent vskill installs to: 56 targets in all. These 39 have a page on this site.
- Claude Code
- Cursor
- GitHub Copilot
- Windsurf
- Codex
- Gemini CLI
- Cline
- Amp
- Roo Code
- Goose
- Continue
- Kilo Code
- Junie
- Trae
- OpenHands
- Qwen Code
- AI Agent
- Tabnine Chat
- Sourcegraph Cody
- Amazon Q
- Firebase Genkit
- Sweep
- Devin
- Phind
- Mentat
- Claude Desktop
- Aider
- GPT Pilot
- Plandex
- bolt.new
- v0
- Replit Agent
- Anse
- TypingMind
- BetterChatGPT
- LibreChat
- Warp AI
- Pieces
- Fig AI
Embed badge
[](https://verified-skill.com/skills/netlify/context-and-tools/netlify-caching)