Nitro Route Rules SWR Cache Invalidation Limits
0 reputation · 13 Feb 2025, 09:17 UTC
0 reputation · 13 Feb 2025, 09:17 UTC
Nuxt utilizes the Nitro server engine to implement route-level caching via routeRules. When configuring a route with swr (stale-while-revalidate), the server is designed to serve a cached response immediately while triggering a background refresh if the TTL has expired.
While the swr mechanism ensures high availability and fast response times, it introduces a period where users receive stale data. In standard configurations, Nitro provides mechanisms to clear the cache, but these often operate on a global scale rather than targeting specific paths or dynamic route parameters.
There is uncertainty regarding the ability to trigger a targeted revalidation for a single specific URL without purging the entire server cache, particularly when deploying to self-hosted Node.js environments versus managed platforms.
30275 reputation · 13 Feb 2025, 19:38 UTC
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.
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.
nitro.config.ts and locate the routeRules entry using swr.
maxAge and staleWhileRevalidate values; adjust if you need tighter or looser caching.
server/api/clear-cache.ts) that receives the target URL and calls nitro.$cache.del(targetUrl).
npx nuxt dev to see console logs like Cache hit /foo or Cache miss /foo.
purge_cache, Vercel: purge, etc.).
nitro.$cache.del(url) only when running a Node.js server; it won’t work on edge runtimes that offload caching.
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.
Use comments to ask for clarification. Post a solution as an answer.
30,275 reputation · 13 Feb 2025, 15:52 UTC
When purging a single SWR‑cached entry you must build the exact key Nitro uses internally. The key consists of three parts separated by double colons: the route rule name (as defined in routeRules), the normalized pathname, and a JSON‑stringified representation of any route parameters. For example, for a rule named products matching /products/42?ref=abc the key is products::/products/42?ref=abc (parameters are already part of the URL). Calling await nitro.$cache.removeItem(key) deletes only that entry, leaving other SWR routes untouched. You can confirm the key exists by logging nitro.$cache.getItem(key) before removal.