UNPKG

astro

Version:

Astro is a modern site builder with web best practices, performance, and DX front-of-mind.

53 lines (52 loc) • 1.31 kB
import { AstroError } from "../../errors/errors.js"; import { CacheNotEnabled } from "../../errors/errors-data.js"; /** * A no-op cache implementation used in dev mode when cache is configured. * The API is available so user code doesn't need conditional checks, * but nothing is actually cached. */ const EMPTY_OPTIONS = Object.freeze({ tags: [] }); export class NoopAstroCache { enabled = false; set() {} get tags() { return []; } get options() { return EMPTY_OPTIONS; } async invalidate() {} } let hasWarned = false; /** * A no-op cache used when cache is not configured. * Logs a warning on first use instead of throwing, so libraries * can call cache methods without needing try/catch. * `invalidate()` still throws since it implies the caller * expects purging to actually work. */ export class DisabledAstroCache { enabled = false; #logger; constructor(logger) { this.#logger = logger; } #warn() { if (!hasWarned) { hasWarned = true; this.#logger?.warn("cache", "`cache.set()` was called but caching is not enabled. Configure a cache provider in your Astro config under `cache` to enable caching."); } } set() { this.#warn(); } get tags() { return []; } get options() { return EMPTY_OPTIONS; } async invalidate() { throw new AstroError(CacheNotEnabled); } }