Route Caching
Astro 7 makes route caching stable for on-demand rendered pages and endpoints.
The core idea: route code says how cacheable a response is, and a cache provider decides how that becomes runtime behavior or host-specific headers.
Mental Model
flowchart LR
route["Page or endpoint"] --> cacheCall["Astro.cache.set"]
middleware[Middleware] --> contextCache["context.cache.set"]
rules[routeRules] --> options["Cache options"]
cacheCall --> options
contextCache --> options
options --> provider{Provider}
provider --> memory["memoryCache runtime cache"]
provider --> cdn["CDN headers and purges"]
API Surface
| API | Use It For |
|---|---|
cache.provider | Enable caching by choosing a provider |
memoryCache() | Built-in in-memory LRU provider for simple single-instance SSR |
Astro.cache.set() | Set cache behavior inside .astro pages |
context.cache.set() | Set cache behavior in API routes and middleware |
routeRules | Configure defaults for route patterns |
cache.invalidate() | Purge by tag or exact path |
Example
import { defineConfig, memoryCache } from 'astro/config';
import node from '@astrojs/node';
export default defineConfig({
adapter: node({ mode: 'standalone' }),
cache: {
provider: memoryCache(),
},
routeRules: {
'/api/[...path]': { swr: 600 },
'/blog/[...slug]': { maxAge: 300, swr: 60, tags: ['blog'] },
},
});
In a page:
---
export const prerender = false;
Astro.cache.set({
maxAge: 120,
swr: 60,
tags: ['home'],
});
---
<h1>Cached page</h1>
Provider Status
| Provider | Status | Best For |
|---|---|---|
memoryCache() | stable built-in | local preview, small single-instance Node deployments, learning the API |
| Netlify CDN provider | experimental | Netlify SSR projects that accept provider churn |
| Vercel CDN provider | experimental | Vercel SSR projects that accept provider churn |
| Cloudflare CDN provider | experimental/private beta dependency | only projects with the required Workers Cache beta access |
Warning: The Astro route caching API is stable, but the CDN providers are not the same maturity level. Do not promise portable CDN invalidation unless you have verified your host provider.
When Not To Use It
Prefer simple HTTP headers when:
- you only need browser/CDN freshness headers
- your deployment has one CDN with existing purge tooling
- you do not need tag/path invalidation from Astro code
- the page is static and should just be prerendered