Cached Route

cachedRoute() wraps a page with an in-memory cache. The handler runs once, and subsequent requests serve the cached result until the TTL expires.

Cached Server Data

This data was generated by the server handler. Refresh the page within 30 seconds — you'll see the same values (cache hit).
generatedAt: 2026-03-01T21:47:21.349Z
randomValue: 0.893206
pid: 4
After 30 seconds, the next request triggers background revalidation (stale-while-revalidate). You'll get the stale data immediately while the cache refreshes.

Cache Invalidation

Use invalidateCache() to clear cached entries by tag or route. The button below calls a server function that invalidates the cache for this page.

Usage

import { cachedRoute, invalidateCache } from "catmint/cache"

// Cache for 30 seconds with a tag
export default cachedRoute(
  async function MyPage() {
    const data = await fetchExpensiveData()
    return <div>{data}</div>
  },
  {
    tag: ["my-page"],
    revalidate: 30,
    staleWhileRevalidate: true, // default
  }
)

// Invalidate by tag:
await invalidateCache({ tag: "my-page" })

// Invalidate by route:
await invalidateCache({ route: "/my-page" })
  • revalidate: TTL in seconds before cache is stale
  • tag: string array for targeted invalidation
  • staleWhileRevalidate: serve stale data while refreshing in background (default: true)
  • Cache key is derived from the handler name + arguments