Skip to content

sharedContentClient

sharedContentClient<T>(create, options?): T

One client for the life of the process, however many times the module holding it is evaluated.

lib/content.ts
import { createContentClient, sharedContentClient } from '@ebitex/content-sdk'
export const content = sharedContentClient(() =>
process.env.CONTENT_DELIVERY_KEY
? createContentClient({ apiKey: process.env.CONTENT_DELIVERY_KEY, site: process.env.CONTENT_SITE_ID })
: null,
)

The factory runs at most once per key, and its result is remembered even when that result is null — an unconfigured site is an answer, not a miss, and retrying it on every request would turn a missing environment variable into a per-request cost. That is why this stores presence rather than truthiness.

Two consequences worth knowing before reaching for it:

  • A pinned client outlives a module reload. Under a dev server’s hot reload, editing the factory does not rebuild the client; restart the process. This is the same trade every pinned database client in a framework app makes, and it is the point rather than a side effect.
  • It is not a cache of configuration. If the factory closes over something that varies per request, the first request’s value is the one every later request gets. Per-request values — a context bag above all — belong on the call, not on the client.

Safe in a browser, where it is simply a memo: a tab has one module graph, so this changes nothing there.

T extends ContentClient | null

() => T

SharedContentClientOptions = {}

T