Configure Route Caching
Start with memoryCache() until you know you need a host CDN provider.
Add A Provider
import { defineConfig, memoryCache } from 'astro/config';
import node from '@astrojs/node';
export default defineConfig({
adapter: node({ mode: 'standalone' }),
cache: {
provider: memoryCache({ max: 500 }),
},
});
memoryCache() is an in-memory LRU provider. It is useful for learning and single-instance deployments. It is not shared across multiple server instances.
Cache A Page
---
export const prerender = false;
Astro.cache.set({
maxAge: 120,
swr: 60,
tags: ['home'],
});
---
<h1>Home</h1>
Cache A Route Group
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 },
'/products/[...slug]': { maxAge: 3600, tags: ['products'] },
},
});
Use Astro route patterns, not glob syntax. Use [...path], not *.
Invalidate From An Endpoint
export async function POST(context) {
await context.cache.invalidate({ tags: ['products'] });
await context.cache.invalidate({ path: '/products/abc' });
return Response.json({ purged: true });
}
Add real authentication before exposing an invalidation endpoint.
Check If Caching Is Active
In development mode, the cache API exists but does not actually cache. Guard optional cache logic with cache.enabled:
---
if (Astro.cache.enabled) {
Astro.cache.set({ maxAge: 300, tags: ['article'] });
}
---
CDN Providers
Only use adapter CDN providers when you have checked current provider status for your host:
| Host | Import | Caveat |
|---|---|---|
| Netlify | @astrojs/netlify/cache | experimental |
| Vercel | @astrojs/vercel/cache | experimental |
| Cloudflare | @astrojs/cloudflare/cache | requires private beta Workers Cache feature |