astro-cms

reduanmasud/astro-cms Checked 2 days ago

Self-hosted CMS for a single Astro project in one GitHub repo: SQLite drafts, live Tiptap+Yjs collaboration, GitHub PR publishing, S3-compatible media, and an…

A small, self-hosted CMS for one Astro project in one GitHub repository. Read both before proposing structural changes. Changing a decision means adding an ADR that supersedes the old one. Do not use Corepack for pnpm 12: the Corepack bundled with Node 24 cannot start it. If pnpm fails with Cannot find module …/corepack/…/pnpm.cjs, run corepack disable pnpm and install it with the command above.

Summary extracted from the README.

Astro CMS

A small, self-hosted CMS for one Astro project in one GitHub repository.

  • GitHub is the source of truth for published Markdown/MDX.
  • Drafts live in SQLite and never touch GitHub.
  • Publishing opens (or updates) one pull request per content item.
  • Editors collaborate live with Tiptap + Yjs + HocusPocus.
  • Media goes to S3-compatible storage (Cloudflare R2, AWS S3, MinIO).
  • MCP is a first-class interface to the same services as the web UI.
  • Auth is one shared password plus a display name. No accounts, teams, or OAuth.

Status: Login, GitHub repository access, Astro collection discovery, SQLite drafts, a Tiptap editor, live collaboration, publishing to GitHub, the media library, and MCP all exist.

Documentation

Read both before proposing structural changes. Changing a decision means adding an ADR that supersedes the old one.

Requirements

  • Node.js 24 or newer. The server runs TypeScript directly through Node's built-in type stripping, so it has no build step.

  • pnpm 12.4.1. Install it with npm:

    npm install --global pnpm@12.4.1
    

    Do not use Corepack for pnpm 12: the Corepack bundled with Node 24 cannot start it. If pnpm fails with Cannot find module …/corepack/…/pnpm.cjs, run corepack disable pnpm and install it with the command above.

  • Docker with Compose, only for the containerized setup.

Configure

cp .env.example .env

Set CMS_PASSWORD (12+ characters) and SESSION_SECRET (32+ characters). openssl rand -base64 32 produces a good secret. The browser never receives either value.

Then connect the one GitHub repository the CMS edits:

  • GITHUB_OWNER and GITHUB_REPOSITORY, e.g. withastro and blog.

  • GITHUB_BASE_BRANCH, the branch pull requests target (default main).

  • GITHUB_TOKEN: a fine-grained personal access token limited to that repository, with read and write access to Contents and Pull requests.

    If GITHUB_OWNER is an organization, not your own username, set Resource owner to that organization when creating the token, not your personal account (the default). A token scoped to your personal account cannot see organization repositories at all, even public ones — GitHub answers every request for one with a 404, indistinguishable from the repo not existing. The organization also has to allow fine-grained tokens in the first place: an org owner can check this under the organization's Settings → Personal access tokens → Settings; some organizations restrict or block them, in which case a classic token with the repo scope is the alternative.

The server checks the token, push access, and base branch at startup, and stops with an explanation if something is wrong.

Develop with Docker Compose

docker compose up --build

Open http://localhost:5173. Compose also starts a HocusPocus server on port 1234 for live editing. The containers reload when you edit files on the host. The API is also reachable at http://localhost:3000/api/health. SQLite data lives in the cms-data volume; docker compose down -v deletes it.

After changing dependencies, rebuild and refresh the containers' node_modules:

docker compose up --build --renew-anon-volumes

Deploy to production with Docker

compose.yaml above is dev-only (bind-mounted source, node --watch, a bundled MinIO with default credentials). compose.prod.yaml builds real images instead — apps/server/Dockerfile, apps/collab/Dockerfile, and apps/web/Dockerfile (a Vite build served by nginx, which also reverse-proxies /api to the server container).

cp .env.example .env   # fill in CMS_PASSWORD, SESSION_SECRET, and GITHUB_*
docker compose -f compose.prod.yaml up --build -d

By default this starts only server and web — the CMS_PASSWORD, SESSION_SECRET, and GitHub values are the only things it requires. It refuses to start if one of those is missing rather than falling back to an insecure default.

HocusPocus (live collaboration) and MinIO (self-hosted media storage) are optional and sit behind Compose profiles, so up never touches them unless asked:

# bring in HocusPocus, MinIO, or both — fill their vars in .env first
docker compose -f compose.prod.yaml --profile hocuspocus --profile minio up --build -d

You can also point HOCUSPOCUS_*/S3_* at your own HocusPocus server or S3/R2 bucket instead of the bundled services — either way, server only enables that feature once its env vars are non-empty.

web publishes port 80 by default (override with WEB_HOST_PORT in .env if something else already holds it); put a TLS-terminating reverse proxy (Caddy, Traefik, nginx) in front of it for a real deployment, since nothing in this file terminates HTTPS itself. HOCUSPOCUS_HOST_PORT, S3_HOST_PORT, and S3_CONSOLE_HOST_PORT are the same escape hatch for the optional services' ports — see .env.example for how each pairs with its matching *_PUBLIC_URL.

Develop on your machine

pnpm install
pnpm dev

pnpm dev starts the API on http://localhost:3000 and Vite on http://localhost:5173. Use the Vite URL; it proxies /api to the API. SQLite is stored in apps/server/data/.

Scripts

Run from the repository root:

Script What it does
pnpm dev API (with --watch) and Vite dev server
pnpm typecheck Type-check every package with TypeScript 7
pnpm lint ESLint, including type-aware rules
pnpm format Format everything with Prettier
pnpm format:check Check formatting without writing
pnpm test Run every package's Vitest suite
pnpm check typecheck, lint, format check, and tests, in order
pnpm build Build the frontend into apps/web/dist

Run a script in one package with pnpm --filter @astro-cms/server test.

Project layout

apps/server/            Hono API + SQLite (@astro-cms/server)
apps/collab/            development HocusPocus server (@astro-cms/collab)
apps/web/               React + Vite frontend and editor (@astro-cms/web)
packages/markdown/      Markdown/MDX <-> editor document (@astro-cms/markdown)
tooling/eslint-config/  shared ESLint config (@astro-cms/eslint-config)
tsconfig.base.json      shared TypeScript options
docs/                   architecture and ADRs

See docs/architecture.md for more.

Contributing

  • Keep it small. Prefer the simplest reliable solution. No new infrastructure (Redis, Postgres, queues, ORMs) without a concrete need and an ADR.
  • Business rules go in apps/server/src/services/. Routes and MCP tools stay thin.
  • New API routes require a session by default. Making one public means adding it to PUBLIC_ROUTES in apps/server/src/app.ts.
  • Server code must be erasable TypeScript (no enum, no parameter properties), and relative imports use the .ts extension.
  • Use interface for object shapes and component props; use type for unions and aliases. Exported functions declare their return types. ESLint enforces both.
  • Put tests next to the code (*.test.ts) and run pnpm check before opening a PR.

Live collaboration

Collaboration is optional. Set all four values to switch it on, or none to have the editor save directly over the API:

Variable Used by Purpose
HOCUSPOCUS_PUBLIC_URL browser WebSocket URL clients connect to
HOCUSPOCUS_INTERNAL_URL server, MCP the same server from inside the network
HOCUSPOCUS_JWT_SECRET both signs and verifies short-lived room tokens
HOCUSPOCUS_WEBHOOK_SECRET both signs the webhook HocusPocus sends back

docker compose up runs one for development. To use your own server, it must verify the CMS's JWT (HS256, the room is the aud claim) and run the standard webhook extension against POST /api/collab/webhook with the create and change events. apps/collab/src/index.ts is that setup in about fifty lines. See ADR-0016.

Frontmatter

Frontmatter renders as controls generated from the collection's schema: a checkbox for a boolean, a date picker for a date, a select for an enum, a tag input for an array of strings, a number input for a number, and a text input (a textarea once a value spans lines) for a plain string. A field whose type has no control yet — object, image, reference, union, literal, unknown, or an array of anything but strings — gets a raw YAML box for that one field, labelled with why. Keys in the file the schema does not mention appear, still editable, in an "Other fields" box.

A control whose value space can't hold the file's current value also falls back to a raw box, even for a type listed above: a date not in YYYY-MM-DD, an enum value outside its declared list, or a number that isn't numeric all render as a silently blank control otherwise, which the raw box avoids by showing the value as YAML instead.

An edit writes back through a YAML document rather than a parse-and-restringify round trip, so comments, key order, and quoting on every key nobody touched survive a save. Whitespace right before an inline comment does not — that is the one thing this cannot preserve.

The whole thing falls back to today's raw YAML box when the collection's schema cannot be inferred, the collection request fails, or the file's frontmatter is not valid YAML at all (ADR-0007, ADR-0022).

Media

Images are stored in an S3-compatible bucket (AWS S3, Cloudflare R2, MinIO) with their metadata in SQLite. Set all six S3_* values to switch it on, or none to leave /api/media returning 404. docker compose up starts MinIO for development; create the bucket once at http://localhost:9001.

Every upload is checked by reading the file's own header, not the browser's Content-Type: PNG, JPEG, GIF, WebP, and AVIF up to 10 MB. SVG is refused because it can carry scripts. Files are stored by content hash, so uploading the same image twice stores it once.

# upload (multipart), list, inspect, delete
curl -b cookies.txt -F file=@hero.png http://localhost:3000/api/media
curl -b cookies.txt 'http://localhost:3000/api/media?unused=true'
curl -b cookies.txt 'http://localhost:3000/api/media/<id>?verify=true'
curl -b cookies.txt -X DELETE -H 'Sec-Fetch-Site: same-origin' \
  http://localhost:3000/api/media/<id>

The CMS tracks which drafts use which file and refuses to delete one that is still referenced. Storage credentials never reach the browser (ADR-0017).

The web interface has a Media page for browsing everything the bucket holds, filtering to files nothing references, and deleting one at a time. Selecting a file shows which drafts and branches use it, and delete stays disabled until that answer comes back empty. There is no upload here: images are still added by pasting or dropping them into a post, because collection removes anything that stays unreferenced past the grace period, and a file uploaded here "for later" would be removed the same way (ADR-0023).

Unused files are deleted automatically, MEDIA_GC_GRACE_DAYS (default 7, minimum 1) after they were last seen with no reference, and only once no draft, no open CMS pull request, and not the base branch refers to them any more. A sweep runs every MEDIA_GC_INTERVAL_HOURS (default 6); set it to 0 to switch collection off. Collection never starts unless media storage is configured. A sweep that cannot establish what is referenced — GitHub unreachable, no content collections found, the base branch missing — deletes nothing and logs why.

Only .md and .mdx files under each collection's content path are scanned for references. A media URL that only appears somewhere else — an .astro component, for instance — is never seen by the scanner and can eventually be collected even while still in use there. Know this before turning collection on (ADR-0021).

Publishing

Drafting never touches GitHub. Publishing is the only thing that does.

Press Publish and the CMS commits the draft to cms/<collection>/<slug> and opens a pull request. Publishing the same draft again adds a commit to that same branch and pull request — never a second one. The CMS never writes your base branch and never merges anything; you review and merge on GitHub.

If the base branch moved since the draft started, publishing stops before writing anything and shows you the file as it stands on the base branch, with a Re-sync button. Re-sync moves the draft's baseline only: it never edits your text, and nothing is merged automatically. This is deliberately strict — any commit to the base branch blocks publishing until you look (ADR-0019).

Once the pull request merges, reopening the document moves it to published.

Enable "automatically delete head branches" on the repository settings. A merged pull request otherwise leaves its branch behind, and the CMS refuses to commit to a branch whose pull request is gone — committing there would reopen merged history.

MCP

Set MCP_TOKEN and the CMS also answers as an MCP server at POST /api/mcp, so a model can do what a person can do in the browser — and no more. Leave it unset and that route answers 404.

MCP_TOKEN=$(openssl rand -hex 32)

Clients authenticate with that token as a bearer token. It is deliberately separate from CMS_PASSWORD: the browser uses the password and a session cookie, an MCP client uses this token, and neither grants the other.

curl -s -X POST http://localhost:3000/api/mcp \
  -H "Authorization: Bearer $MCP_TOKEN" \
  -H 'Content-Type: application/json' \
  -H 'Accept: application/json, text/event-stream' \
  -d '{"jsonrpc":"2.0","id":1,"method":"tools/list","params":{}}'

The tools cover the same ground as the web UI: read the project and its collections, list, search, create, update and delete drafts, list, upload and delete media, then publish — which opens or updates the same pull request the browser would, and stops on drift the same way.

upload_media takes a public URL, not file bytes: the server fetches the image and stores it, then returns the public URL to embed in content. It refuses anything but http/https, refuses addresses that are not publicly routable, and does not follow redirects (ADR-0020).

Changes made through MCP are attributed to a collaborator named MCP, so they read like anyone else's in the history.

Known limitations

  • The login rate limit (10 failed attempts per 15 minutes) is keyed by the TCP peer address. Behind a reverse proxy every client shares one address, so enough failed attempts from anyone block everyone until the window ends.
  • ESLint parses with TypeScript 6.0 while the project type-checks with TypeScript 7, because typescript-eslint does not support 7 yet (ADR-0012).