Answer the question first
In a Nitro‑powered Nuxt 3 application you can delete a single SWR‑cached entry by calling the cache instance’s del (or delete) method with the exact request URL. This removes only that entry; other cached routes remain untouched. Nitro does not expose a dedicated “purge‑by‑route” endpoint, so the recommended approach is to use the internal nitro.$cache API in a server route or middleware.
Does Nitro support a targeted purge for dynamic routes?
Yes, but only for the internal cache that Nitro manages on the server. The key is the full request URL, including query string and path parameters, so you must construct that string exactly. For example:
export default defineEventHandler((event) => {
const url = event.node.req.url; // e.g. "/products/42?ref=abc"
nitro.$cache.del(url); // removes only this entry
return { message: 'Cache cleared for ' + url };
});
On edge runtimes that delegate caching to a CDN (e.g., Cloudflare Workers, Vercel Edge), Nitro’s internal cache is not used; you must call the provider’s purge API instead. Nitro does not currently offer a cross‑runtime, route‑level purge API.
Step‑by‑step for a Node.js deployment
- Open
nitro.config.ts and locate the routeRules entry using swr.
- Confirm the
maxAge and staleWhileRevalidate values; adjust if you need tighter or looser caching.
- Create a server route (e.g.,
server/api/clear-cache.ts) that receives the target URL and calls nitro.$cache.del(targetUrl).
- Deploy and test by requesting a cached page, then hitting the clear‑cache route and verifying the next request is a cache miss.
- Use
npx nuxt dev to see console logs like Cache hit /foo or Cache miss /foo.
Step‑by‑step for an edge runtime
- Identify the CDN provider’s cache purge API (Cloudflare:
purge_cache, Vercel: purge, etc.).
- Construct the full URL you want to purge and call the provider’s endpoint from a serverless function or API route.
- Verify the purge by checking the provider’s dashboard or by making a request that should now be a miss.
Key points to remember
- Use
nitro.$cache.del(url) only when running a Node.js server; it won’t work on edge runtimes that offload caching.
- Dynamic route keys include path parameters; if you only know the base path, you’ll need to match all possible parameter values.
- There is no hard limit on the number of SWR entries, but the cache size is bounded by the runtime’s memory and provider limits.
- Always test in a staging environment before clearing production cache entries.
Missing diagnostic detail
Which Nitro runtime are you deploying to (Node, Cloudflare Workers, Vercel Edge, etc.)? This will determine whether you can use nitro.$cache.del or must call an external purge API.