# The package

`@borkol/content` is the npm package a site repo uses to talk to Borkol. It holds the schema DSL (`borkol.content.ts`), a typed REST client, the `<Sections>` renderer, the Next.js helpers and the `borkol` CLI. It is MIT licensed and has no runtime dependencies; `next` (16 or newer) and `react` (18 or newer) are optional peers.

## Install

```bash title="Install"
pnpm add @borkol/content
```

The CLI needs **Node 22.18 or newer**: it loads `borkol.content.ts` through Node's own type stripping, with no loader to install. On an older Node every command but `help` stops with `borkol needs Node 22.18 or newer to load borkol.content.ts`.

## Set up a site

```bash title="Set up"
npx borkol init --site bakery-web
```

`--site` takes the site key from Settings > Website. `init` writes:

| File | Holds |
| --- | --- |
| `borkol.content.ts` | A starting schema: the `site-settings` and `navigation` singletons, a `hero` section and the page body. |
| `app/api/borkol/route.ts` | The webhook route, `export const POST = webhookHandler()`. |
| `app/api/borkol/preview/route.ts` | The preview route, `export const GET = previewHandler()`. |
| `app/layout.tsx` | `<BorkolPreview appOrigin={...} />` after `{children}` in the root layout (a new layout when there is none). |
| `components/borkol/sections.tsx` | The sections registry, one component per section key: `{ hero: Hero }`. |
| `.env.example` | `BORKOL_API_URL`, `BORKOL_SITE_KEY`, `BORKOL_WEBHOOK_SECRET`, `BORKOL_APP_ORIGIN` and a commented `BORKOL_PUSH_KEY` line. |
| `AGENTS.md` | A block with the [rules for the agent](/api/build-a-site/index#rules-for-the-agent) and a link to these pages. |

With a `src/` layout the routes, the layout and the registry go under `src/`. An existing root layout gains the import and the tag only when it has exactly one `{children}` before `</body>`; otherwise the line to add is printed. A file that exists is skipped and named; `.env.example`, `.env.local`, `.gitignore` and `AGENTS.md` only gain the lines they lack. `.env.local` gets empty `BORKOL_*` lines, and `.env.local` and `.borkol/` are kept out of git. When `next.config.*` ends in exactly `export default nextConfig`, that line becomes `export default withBorkol(nextConfig)`; any other config (or none) is left alone and the line to add is printed.

## The CLI

| Command | Does |
| --- | --- |
| `borkol init [--site <key>]` | Writes the files above. |
| `borkol push [--dry-run] [--yes]` | Uploads changed section thumbnails (cached by sha256 in `.borkol/thumbnails.json`), then plans (`--dry-run`) or applies `borkol.content.ts` through [`PUT /v1/api/objects/schema`](/api/build-a-site/schema#put-v1apiobjectsschema) and prints the plan: `+ type bread (4 properties)`, `~ page.body allowed sections: hero, bread_carousel`, `0 archives, 0 refused`. `--yes` allows archives that hide stored values. |
| `borkol seed [--dry-run] [--publish] [--overwrite]` | Sends `borkol.seed.json` and the images it names as one [seed](/api/build-a-site/seed) request, then follows the run: `3 of 14 images` on one line, then `9 records created` (`and published` with `--publish`). |
| `borkol pull --check` | Lists what differs between `borkol.content.ts` and the store, property order included. |
| `borkol pull --write [--force]` | Writes `borkol.content.ts` from the store's schema, to adopt an existing store. Refuses to overwrite the file without `--force`. |
| `borkol doctor [--url <site>]` | Checks the whole setup, see [doctor](#doctor). |
| `borkol help` | The usage text. |

### Environment

The CLI reads the shell environment, then `.env.local`, then `.env` (an empty line never hides a value from another file). It never prints a key, a secret or the API URL.

| Variable | Used by |
| --- | --- |
| `BORKOL_API_URL` | Every command. Default `https://api.borkol.com`. |
| `BORKOL_PUSH_KEY` | `push`, `seed`, `pull` and `doctor`. Only on the owner's machine: never committed, never in the deployed env. |
| `BORKOL_SITE_KEY` | `pull` when no push key is set, and `doctor`. Also in the deployed env of the site, where `createFetchers` and `previewHandler` read it (with `BORKOL_API_URL`). |
| `BORKOL_WEBHOOK_SECRET` | `doctor` signs its webhook ping with it. Also in the deployed env of the site. |
| `BORKOL_APP_ORIGIN` | The dashboard origin `withBorkol`, `<BorkolPreview/>` and `doctor` allow, and the link `seed` prints. Default `https://app.borkol.com`. Set it on the site at build time too: the layout reads it while the static shell renders. |
| `BORKOL_SITE_URL` | `doctor`, when `--url` is not given. Default: the site URL saved under Settings > Website. |

### Exit codes

| Code | Means |
| --- | --- |
| `0` | Done. A `doctor` run passed every check. |
| `1` | An error: a missing key or file, a mistake in `borkol.content.ts` or `borkol.seed.json`, Borkol unreachable, a failed seed run, or a failed `doctor` check. |
| `2` | Borkol refused: drift (`pull --check`), a plan with a refused op, archives without `--yes`, or a seed whose `--publish` or `--overwrite` was refused. |

### Seed

`borkol seed` reads `borkol.seed.json` at the repo root: a JSON list of records in the [import record format](/api/build-a-site/seed#the-records), or `{"records": [...]}`. An image is `{"src": "...", "alt": "..."}`; a `src` that is not an http(s) URL is a path relative to the repo root and is sent as the file part `files[<src>]`. A path outside the repo, or one that does not exist, stops the seed before anything is sent.

```json title="borkol.seed.json"
[
  {
    "external_id": "rye",
    "type": "bread",
    "slugs": { "en": "rye" },
    "values": { "name": { "en": "Rye" }, "photo": { "src": "images/rye.jpg", "alt": "A rye loaf" } }
  }
]
```

The run is queued, so the CLI polls its progress every second: in a terminal one line is rewritten in place, in a log every change gets a line. After 15 minutes, or when a poll fails, it stops with exit `1` and prints the job id; the run may still complete in Borkol's queue. When the run ends it prints the records created, names every image that could not be stored, and on a failed run lists each error (no record is created then). Seeded records are drafts: without `--publish` the CLI says so and prints the dashboard list of each seeded type, where the bulk **Publish** action publishes them. `--publish` and `--overwrite` are only for a fresh store: once a person has edited the content, Borkol refuses them (`409 seed_locked`), the CLI prints the reason and exits `2`, and `borkol seed` without them still adds what is missing.

### Doctor

`borkol doctor` runs every check in this order and prints one line per check, `ok`, `fail` with the reason and a `fix:` line, or `skip`. Any `fail` ends on exit `1`.

| Check | Passes when |
| --- | --- |
| `site key` | `BORKOL_SITE_KEY` is set, is not the push key, and reads `GET /v1/api/objects/schema`. |
| `push key` | `BORKOL_PUSH_KEY` is set, reads the schema, and a dry-run push answers `200` (the key carries `objects:schema`) without a refused op. A push that needs `--yes` adds a `warn:` line. |
| `webhook` | A body signed with `BORKOL_WEBHOOK_SECRET` (`X-Borkol-Signature`) posted to `{site}/api/borkol` answers `200`. |
| `preview` | A token minted with the push key (`POST /v1/api/objects/preview-tokens`, the home page) opened at `{site}{preview_path}?token=...` answers `307` to a path on the site carrying `borkol_preview`, with `Referrer-Policy: no-referrer`. The token is never printed. |
| `frame-ancestors` | `GET {site}/` sends a `Content-Security-Policy` whose `frame-ancestors` names `BORKOL_APP_ORIGIN`. |
| `cacheComponents` | The resolved `next.config.*` (loaded through Node's type stripping, a config function called with the build phase) has `cacheComponents: true`. A config that does not load in plain Node gets a text check and a `warn:` line. The fetchers use `'use cache'`, which Next 16 rejects without it. |
| `sections` | Every section key of `borkol.content.ts` (its own sections and every key a sections field allows) has an entry in `components/borkol/sections.tsx`, matched as a property name like `hero:`. |
| `thumbnails` | Every `defineSection` has a thumbnail and the file exists. |
| `drift` | `pull --check` finds no difference. |

`{site}` is `--url`, else `BORKOL_SITE_URL`, else the site URL saved under Settings > Website. Run it against the local dev server with `borkol doctor --url http://localhost:3000`, and against the deployed site before reporting done.

## Next.js

```ts title="next.config.ts"
import type { NextConfig } from "next";
import { withBorkol } from "@borkol/content/next";

const nextConfig: NextConfig = {};

export default withBorkol(nextConfig);
```

`withBorkol(config, { appOrigin? })` adds `Content-Security-Policy: frame-ancestors 'self' <origin>` for every path to the config's own `headers()`, so the dashboard can show the site in its preview. The origin is `appOrigin`, else `BORKOL_APP_ORIGIN`, else `https://app.borkol.com`. It also sends `X-Robots-Tag: noindex` and `Referrer-Policy: no-referrer` on every URL with `borkol_preview`, and turns on `cacheComponents` (with `partialPrefetching` on Next 16.3 or later, as the Next.js docs pair them; on an older Next it stays unset) unless the config sets `cacheComponents` itself; `cacheComponents: false` stays off. It takes a config object or a config function.

```ts title="app/api/borkol/route.ts"
import { webhookHandler } from "@borkol/content/next";

export const POST = webhookHandler();
```

`webhookHandler({ secret? })` checks `X-Borkol-Signature` (the hex HMAC-SHA256 of the raw body with `BORKOL_WEBHOOK_SECRET`, compared in constant time). A valid event calls `revalidateTag("borkol", { expire: 0 })` and answers `200 {"ok": true}`; a wrong or missing signature answers `401` and flushes nothing; no secret on the site answers `500`. Every Borkol fetcher caches under the `borkol` tag, so one event refreshes every page. A visitor whose open tab already holds a page may keep seeing the old version until the client router cache expires (up to about 5 minutes for prefetched static parts, [`staleTimes`](https://nextjs.org/docs/app/api-reference/config/next-config-js/staleTimes)); a reload or a new visit shows the new content at once. See [Webhooks](/api/webhooks/index).

### Fetchers

```ts title="lib/borkol.ts"
import { createFetchers } from "@borkol/content/next";
import type content from "../borkol.content";

export const borkol = createFetchers<typeof content>();
```

`createFetchers({ url?, key? })` (default `BORKOL_API_URL` and `BORKOL_SITE_KEY`) returns the client's getters. Without a preview token each read runs inside `'use cache'` with `cacheTag("borkol")` and `cacheLife("max")`, so it is cached until the webhook flushes the tag; the cache key holds a fingerprint of the key, never the key. With `{ preview }` the read runs uncached and sends `X-Borkol-Preview`. Pages read `searchParams.borkol_preview` inside `<Suspense>` and pass it on, see [Preview](/api/build-a-site/preview#the-token-in-the-url-passed-to-fetchers).

### Preview

```ts title="app/api/borkol/preview/route.ts"
import { previewHandler } from "@borkol/content/next";

export const GET = previewHandler();
```

`previewHandler({ url?, key? })` checks the link's token with Borkol and redirects (`307`) only to the record's own path with `?borkol_preview=<token>`, never to the link's `path`. See [The preview route](/api/build-a-site/preview#the-preview-route).

```tsx title="app/layout.tsx"
import { BorkolPreview } from "@borkol/content/next";

// inside <body>, after {children}:
<BorkolPreview appOrigin={process.env.BORKOL_APP_ORIGIN ?? "https://app.borkol.com"} />
```

`<BorkolPreview appOrigin />` answers `borkol:ready` to the dashboard and calls `router.refresh()` on its `borkol:refresh`, accepted only from `appOrigin` and `window.parent`. See [Refresh after an autosave](/api/build-a-site/preview#refresh-after-an-autosave).

### Several instances share no cache

Next.js keeps the `'use cache'` cache in the memory of each process. A Borkol event reaches one instance, so only that instance flushes the `borkol` tag; the others keep serving the old content until they restart. One instance (the default on a single Coolify or `next start` deploy) needs nothing. With several instances behind a load balancer, or on serverless compute, configure a shared cache handler with [`cacheHandlers`](https://nextjs.org/docs/app/api-reference/config/next-config-js/cacheHandlers) (for example on Redis, with tag coordination across instances), or the dashboard can show **Live** while some visitors still see the old page.
