Skip to content

v5.0.0

Latest

Choose a tag to compare

@dulnan dulnan released this 04 Nov 12:04
· 16 commits to main since this release

Most changes in 5.0 fix caching bugs: they change what is cached and for how long, and neither the build nor the type checker notices them. This release also hardens the module against cache poisoning and adds a Cache-Status header, agent skills and Cloudflare Workers support.

Upgrade with your coding agent

The package now ships two agent skills for coding agents such as Claude Code, Cursor or Codex:

  • nuxt-multi-cache-upgrade-v5 checks every breaking and behavior change below against your project and reports each one with evidence.
  • nuxt-multi-cache helps with adding, reviewing and debugging caching.

Install 5.0 first, then link the skills into your agent's skill folder:

# pnpm 12.11+
pnpm approve

# npm, yarn, bun
npm install -D skills-npm && npx skills-npm setup

# or from the repository
npx skills add dulnan/nuxt-multi-cache

Or just symlink from node_modules/nuxt-multi-cache/skills/*.

Then ask your agent:

Upgrade nuxt-multi-cache to 5.0 and check every breaking change against this project. Report each one as affected or not affected.

The skill fixes what is mechanical and lists everything else for you to decide, including what it can't check in the repository, such as environment variables of the deployment.

Requirements

  • Nuxt ^4.0.0. On Nuxt 3, the module isn't loaded and Nuxt logs a warning.
  • Node ^20.19.0 || >=22.12.0, required by @nuxt/kit 4.6 and unplugin 3.
  • The module's runtime dependencies (@nuxt/kit, h3, unstorage, consola, …) are now declared, so strict installs (pnpm without hoisting) work.

New features

  • Cache-Status header (RFC 9211) for the route cache with route.cacheStatus, e.g. nuxt-multi-cache; hit; ttl=376. It can be turned on at runtime with NUXT_MULTI_CACHE_ROUTE_CACHE_STATUS. (#102)
  • CDN helper: addCustomDirective() (#123, thanks @lukereative), getControl() with the new CDNCacheControl type, and must-understand.
  • Server options in Nuxt layers. (#124)
  • Cloudflare Workers work and are tested in CI with Miniflare. (#104)
  • Agent skills (see above).

Breaking changes

  • A max age of 0 means "never" in the data cache. addToCache(value, tags, 0), helper.setMaxAge(0) in useDataCacheCallback and serverMaxAge: 0 used to store the item permanently. Use 'permanent' (or -1) for that.
  • useCachedAsyncData without serverMaxAge doesn't cache on the server. It cached permanently. Items that 4.x stored for it are no longer read.
  • Uncacheable items bubble. With bubbleCacheability, a component or data cache item marked as uncacheable makes the route uncacheable and the CDN header max-age=0, no-store. useDataCacheCallback no longer stores a result after setUncacheable().
  • A <RenderCacheable> child with slot content needs a cache-key, because the generated key only contains the props. This includes <NuxtLink>text</NuxtLink>, <Transition>, named slots and a nested <RenderCacheable>. Lazy components (<LazyFoo>) need one too: they all had the same name, so two of them could render each other's markup.
  • A cacheKeyPrefix method that returns no prefix gets the prefix {none}. Before, such a request's key could address the items of another prefix. Existing items of these requests are missed once, and purging them by key needs {none}--<key>.
  • Permanent items get no storage TTL. Redis, Upstash and Vercel KV rejected the old ttl: -1, so permanent route and component items were never stored; Cloudflare KV expired them after 60 s. Now they stay until purged. The TTL lasts until the later of the max age and the stale-if-error expiry, and stale-while-revalidate routes get no TTL.
  • A set API token is required, even with authorization disabled. A build with authorization: false and a token from runtime config had an open API. To disable authorization at runtime, also set NUXT_MULTI_CACHE_API_AUTHORIZATION_TOKEN= (empty).
  • Types: useMultiCacheApp() returns MultiCacheApp | undefined (and NitroApp.multiCache is optional); DataCacheItem.data is unknown; state.keysBeingRevalidated is a Map; CacheabilityInterface has a new getCacheable() method; the CDN helper uses @tusbar/cache-control 3.x.

Behavior changes

Max age and the data cache

  • Negative max ages other than -1, and NaN, mean "never" (they meant "permanent"). Infinity means "permanent" (it was never served).
  • 'permanent' no longer raises a lower max age set before.
  • A bubbled item without stale-if-error keeps the page's stale-if-error.
  • clientMaxAge of useCachedAsyncData accepts every max age, such as '5m' and 'permanent'. An invalid string now throws on the client too.
  • Falsy cached values (0, false, '', null) are cache hits in useDataCacheCallback and useCachedAsyncData.
  • The data cache stores items with max age 0 and stale-if-error, so staleValue works for them.
  • With bubbleCacheability, the values passed to addToCache() bubble even if the data cache doesn't store the item: disabled at runtime, enabledForRequest false, a key with ?, a failed read. Pages then get the data's max age and tags.

Component cache

  • A wrapper that forwards its slot into <RenderCacheable> is cached if it sets a cache-key, and so is a v-for or a multi-root <template> as the first node. Don't wrap a layout's page <slot /> this way: it caches whole pages without their head tags and payload.
  • Nested <RenderCacheable> instances merge into the outer one: an inner no-cache or uncacheable instance makes the outer one uncacheable, and the inner max age and tags apply to the outer item.
  • Each cached component stores the modules of its own render, so a cache hit renders the right styles and preload links.
  • Generated keys escape quotes and backslashes in prop values and :, , and \ in object keys. Keys of props without them are unchanged.
  • Keys with a ? are neither read nor stored.

Route cache

  • Only GET and HEAD requests are served from and stored in the route cache. API routes requested with other methods during SSR still pass their route cache values to the page through useCacheAwareFetchInterceptor.
  • The module API, Nuxt's build assets and the error page never use the route cache. Paths below api.prefix are always excluded.
  • Static files and paths excluded by route.applies skip the route cache entirely, also the lookup. The default static file check ignores the query string, and applies gets the decoded path.
  • Requests with an ambiguous path (//contact, a backslash, a . or .. segment, an encoded ?) don't use the route cache.
  • Route keys with a ? (from buildCacheKey) are neither read nor stored.
  • The root path is stored as {root} (with unstorage 1.17.6, it wasn't cached at all).
  • An enabledForRequest that throws no longer fails the request: the page is rendered without the route cache.
  • Internal headers of API routes are no longer stored with the route.
  • A stale-while-revalidate revalidation counts for 60 seconds at most, and a stale route is no longer served after a revalidation that wasn't stored.

Stale if error

  • A stale route is only served for server errors (5xx and unhandled errors), not for a 404.
  • A route with a permanent stale-if-error is served stale.
  • Stale routes are served from a Nitro error handler: browsers get the cached status code (it was the error status), the default status text, and the headers of a cache hit. Cookies and headers set by the failed request are no longer sent along. The app's error handler isn't called for them; Nitro's error hook still is.
  • Errors that don't end the request (event.captureError) no longer serve the stale route.
  • Pages with experimental.inlineErrorRendering are served stale too.

Fetch interceptor

  • Internal requests are marked with a random token instead of true, and only for relative URLs. Requests to absolute URLs no longer bubble route cache values (their CDN headers still merge). The header is removed from the request once recognized, so a proxy route rule can't forward the token.
  • The page gets the route cache max age, stale-if-error and uncacheability of the API routes it fetches, not only their tags.
  • No empty cache tag from API routes without route cache tags, and the tags of a retried request are kept.

CDN headers

  • Cache-Control gets private when the CDN header is private or no-store, because Fastly ignores both in Surrogate-Control. Turn it off with cdn.privateCacheControl: false.
  • s-maxage is capped by all sources: merged responses, bubbled items and the page's own max age. A bubbled "uncacheable" adds no-store.
  • 'permanent' no longer writes max-age=-1, and NaN writes max-age=0.
  • Merged headers are parsed with @tusbar/cache-control 3.x: a private="Set-Cookie" from an API route makes the page private.
  • NUXT_MULTI_CACHE_CDN=false turns off the CDN headers. Before, only the interceptor read it.

API and runtime config

  • NUXT_MULTI_CACHE_API_ENABLED=false disables the API endpoints (404). It was ignored.
  • Runtime config can enable a feature that has enabled: false: a feature is added to the build when its key exists in the module options. The startup table says "Added in Build" and has an API row.
  • cacheTagInvalidationDelay: 0 purges during the request (it became 60000). cacheTagInvalidator.add() returns a promise. On Cloudflare presets the delay is always 0, because Workers don't run timers after the response.
  • API responses are sent with Cache-Control: private, no-store, a private CDN header, X-Content-Type-Options: nosniff and a sandbox CSP. The inspect endpoint sends route and component items as text/plain.
  • A multiCache.serverOptions.ts in a Nuxt layer is used if the app has none.

Logging

  • The module's server logs appear. In 4.x its runtime logger printed nothing, so storage and purge errors were silent. With debug: true, every cache operation is logged.

Security

5.0 closes several ways to get content cached for the wrong request

  • Cache poisoning through paths that the router and the cache key read differently (//contact, dot segments, encoded ?), through keys cut off at ?, through crafted props of components with generated keys, and through the prefix of a cacheKeyPrefix method.
  • Authorized API responses could be route cached or CDN cached and served to anyone, and inspected markup ran in the browser. Because the module doesn't cache anything by default, you would have to marked this response as cacheable, for example through a server middleware (which you really shouldn't)
  • Any client could request the internal cache headers of API routes (e.g. cache tags)
  • When a failed request returned a stale route, it would send a set-cookie header from the failed request
  • A build with authorization: false and a runtime token had an open API. You now have to explicitly set an empty token as well.

After upgrading

Purge all caches after deploying (POST /__nuxt_multi_cache/purge/all, or the configured api.prefix) if you use a persistent storage such as Redis. Items stored by 4.x keep working, but keys changed, and some items have a wrong expiry, the internal headers of API routes, the modules of other components or request-specific nested markup.

The upgrade skill has the details and the checks for each change.