sharedContentClient
sharedContentClient<
T>(create,options?):T
One client for the life of the process, however many times the module holding it is evaluated.
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.
Type Parameters
Section titled “Type Parameters”T extends ContentClient | null
Parameters
Section titled “Parameters”create
Section titled “create”() => T
options?
Section titled “options?”SharedContentClientOptions = {}
Returns
Section titled “Returns”T