Writing a data source
The data source contract is stable in v1: a plugin written against definePlugin (sources,
options…), defineClientPlugin, SecretRefSchema / readSecret, PluginSource,
PluginSourceContext, PluginSourceResult and DataSourceMeta keeps working across Récit 1.x
(full symbol and diagnostic list in Stability).
The rest of the plugin API — directives, components, film blocks, themes, exporters, commands, AI
jobs, and internal loaders such as findPluginSource — remains experimental
(Stability).
Minimal plugin
A data source plugin needs a Node entry (src/index.ts), the lazy module of each source
(src/rows.ts), and a browser entry (src/) without schemas or Node imports so a site can
evaluate recit. in the browser (Writing a plugin):
TypeScript
TypeScript
TypeScript
recit/plugin and @recit/ export the same definePlugin, defineClientPlugin,
PluginSource, PluginSourceContext, PluginSourceResult, DataSourceMeta,
DataSourceMetaSchema, DataCell, DataColumnType, SecretRefSchema, SecretRef and
readSecret (Stability).
Using the source in a story
Register the plugin in recit.:
TypeScript
Then declare an entry with source: under data: in a story (or in a site page):
Markdown
In the v1 JSON Schema, DataPluginSourceSchema (x-) checks source;
at build time, Récit validates the remaining keys (dataset, limit) with the source's schema
before calling read.
Context and result
read(context) receives a PluginSourceContext:
| Field | Type | Meaning |
|---|---|---|
name | string | Key of the table under data: (for example "visits"). |
entry | Record<string, unknown> | Keys of the data: entry without source, validated by schema. |
options | unknown | Plugin options passed in recit., validated by definePlugin's options schema. |
env | Readonly<Record<string, string | undefined>> | Environment dictionary for readSecret(secret, context.env). |
baseDir | string | Directory of the story file. |
signal | AbortSignal | undefined | Optional in the type so unit tests can call read without a controller; always provided by Récit and aborted when the read timeout (30 s) expires. |
read(context) resolves to a PluginSourceResult:
| Field | Type | Meaning |
|---|---|---|
rows | Record<string, DataCell>[] | Table rows (string, number, boolean or null cells). |
columns | Record<string, DataColumnType>? | Optional column types ("string", "number", "date", "boolean"); inferred from the rows when omitted. |
meta | DataSourceMeta? | Optional dataset provenance using DCAT field names: title, url, publisher, license, modified (ISO date or date-time). |
Récit validates and caches meta alongside rows. When an author writes ::chart without a
source= attribute, @recit/data composes the Source: caption from publisher, title and
license.
Execution rules, cache and diagnostics
Every plugin data source runs in Node at build time and hands inline rows to @recit/data; nothing
runs in the browser.
- 30 s timeout and
signal. Eachreadcall is bounded to 30 seconds. When the deadline expires, Récit abortscontext.and fails the reading (signal took longer than 30 s). Pass{ signal:tocontext. signal } fetchso in-flight requests stop immediately. - 100,000 rows. Returning more than 100,000 rows fails the reading without truncating rows or
writing an oversized table to
.recit/.cache/ - Cache and
ttl. Valid readings are written to.recit/forcache/ data/ ttlseconds (3600by default;ttl: 0reads on every build without reading or writing the cache). The cache file key hashes the entry, the plugin'soptionsandversion, and the values of any referenced secrets (secret values never appear in the file name). - Future timestamps. A cache entry whose
readAttimestamp is in the future (readAt > now()) is never treated as fresh: Récit re-reads the source, while keeping that cache file available as aplugin-stalefallback if the fresh read fails. recit build --. Bypasses fresh cache hits and callsrefresh readagain for every plugin data source.- Offline fallback and diagnostics.
plugin-stale(warning):readfailed (network error, timeout, row limit or invalid rows) and a valid cached reading within the row limit exists on disk — even pastttl, with a futurereadAt, or under--refresh. Récit serves the cached table and warns with the cache date.plugin-(error):failed readfailed and no valid cached reading exists; the build fails.plugin-(error):missing data:names asourcethat no plugin inrecit.declares.config.ts plugin-(error): the entry's keys fail the source'soptions- invalid schema, or the plugin's options failoptions.plugin-(error): in a site, the plugin package has nono- browser- entry browserexport (defineClientPlugin).
recit validate. On a deck (in Markdown or in its JSON twin) that declaresdata:,recit validateresolves its plugin sources and reportsplugin-,failed plugin-staleandplugin-alongsidemissing plugin-. A project whose plugins declare only fields of the data source contract (options- invalid name,api,version,options,sources) does not receive aplugin-warning.experimental
Secrets
Never accept a raw token or API key in recit. or in story front matter, and never read
process.env at module import time. Declare every secret with SecretRefSchema ({ env:)
and resolve it inside read with readSecret(secret, context.env):
TypeScript
In a story, the author writes token:. Only the validated rows and meta
reach the rendered page.
Testing without network
Test read directly without touching the network — either by accepting a custom fetch function in
the plugin's options, or with vi. in Vitest:
TypeScript
Passing an optional fetch in options (marked internal in your TypeScript type) works without
global stubs and lets tests run concurrently.
Reading the package version at runtime
definePlugin's version is part of the .recit/ key: when a plugin ships a fix to its row
normalisation, bumping the package version invalidates cached readings automatically.
Do not read "version" with a static import pkg from "../: bundlers inline that
value at build time, before a release step bumps package.json. Resolve and read package.json at
runtime instead:
TypeScript
Real-world example
@recit/ implements the v1 data source contract for French public open data on
data.gouv.fr (source:, ttl: 86_400, no token): it fetches data/json/ and
profile/ column types from tabular- (downgrading mixed columns to "string"),
falls back to raw CSV via the catalogue API (www.) on HTTP 404, populates DCAT
meta (title, publisher, license, modified, url), reads its package.json version at
runtime, accepts an injectable fetch in options for offline unit tests, and ships a zero-schema
browser entry.