Caching

Store parsed bodies for fast reads.

Without a cache, every content.get() re-parses its document from the source. The cache stores parsed document bodies and the manifest index so repeated reads and cold starts stay cheap. It's on by default (in-memory) and fully optional.

Bodies are populated lazily: the first get() for a path parses and stores it. Later reads return the stored copy until it's invalidated or expires.

Options

Configure the cache with a CacheOptions object on comarkContent, or pass cache: false to disable it:

CacheOptions.ts
interface CacheOptions {
  driver?: Driver
  ttl?: number
  strategy?: 'swr' | 'none'
  loadManifest?: () => Promise<CacheArtifact | null>
  loadSnapshot?: (source: string) => Promise<CacheArtifact | null>
}
Every field is optional. The defaults (an in-memory driver, the swr strategy, and no TTL) need no setup.
OptionTypeDefaultDescription
driverDriverin-memoryunstorage driver backing the cache.
ttlnumberundefinedMilliseconds before an entry is stale. Omit to never expire.
strategy'swr' | 'none''swr'How a stale entry is served.
loadManifest()() => Promise<CacheArtifact>undefinedHydrate the manifest from a manifest artifact.
loadSnapshot()(source) => Promise<CacheArtifact>undefinedHydrate a source's bodies from a snapshot.

driver

Any unstorage driver. In-memory by default (per instance); swap for Redis, a Vercel Runtime Cache, or similar to share the cache across instances.

ttl

Milliseconds before a cached entry is considered stale. Omit it (the default) and entries never expire. What happens once stale is set by strategy.

strategy

swr (default) serves a stale entry immediately and re-reads the source in the background, so fresh content lands on the next read. none skips the background refresh: a stale entry is treated as a miss and re-read on the next get().

loadManifest()

Hydrate the manifest from a pre-built manifest artifact on content.init(), so a cold instance starts warm. See Artifacts & hydration.

loadSnapshot()

Hydrate a source's bodies from a snapshot the first time it's read. See Artifacts & hydration.

Check the caching section for more details about the different cache strategies and how to refresh a source on demand.