The event relay was an in-memory array that started empty on every process, so synced history vanished on every API-server restart. Persist it to a gitignored .data/events.sqlite3 via node:sqlite like the blob store (EVENT_DB_PATH override; seq rowid pull cursor; UNIQUE(authorPubkey,signature) push idempotency), keeping the exact exported API. Synced history now survives restarts and keeps growing, and a fresh device pulls the whole lived-in history on load; ShellLayout adds a light 6s poll for live cross-device reflection. Adds a dev:all script, isolates the two integration tests to a temp EVENT_DB_PATH, documents EVENT_DB_PATH.
5.8 KiB
Self-hosting THE COMMONS
This app is designed to be self-hosted on your own machine or server - no managed cloud database, no third-party storage account, no external credential of any kind. Everything below runs with tools already in this repo and Node's built-ins.
What needs a server at all
Almost everything in this app is a client-only PWA (identity, event log,
ledger all live in the browser's IndexedDB and keep working offline). Two
small pieces use the long-running API process, both persisting to local
SQLite files via Node's built-in node:sqlite module - no database server,
no managed service, no signup required:
- the blob store (
api/blob/*,api/_lib/blobStore.ts), for cross-device mini-app sharing (BLOB_DB_PATH, default./.data/blobs.sqlite3); - the signed-event relay (
api/sync,api/_lib/eventRelayStore.ts), the best-effort cross-device sync for the event log. It survives restarts, so synced history keeps accumulating and a fresh device pulls the whole "lived-in" history on load (EVENT_DB_PATH, default./.data/events.sqlite3). The relay only ever stores signature-verified events and never blocks a local write - the event-log append/read/replay path itself stays fully offline.
1. Build the frontend
npm ci
npm run build
This produces the static site in dist/.
2. Run the API as a long-running process
api/_lib/devServer.ts already implements a plain node:http server that
serves api/blob and api/blob/[id] by calling the same handler functions
Vercel's serverless runtime would call - it just does it as a normal
long-running process instead of per-request cold starts. Running that same
file directly with node (via its own if (import.meta.url === ...) entry
point) gives you a minimal production-equivalent entry point:
PORT=3001 \
BLOB_DB_PATH=/var/lib/commons/blobs.sqlite3 \
EVENT_DB_PATH=/var/lib/commons/events.sqlite3 \
node api/_lib/devServer.ts
(This is the same command npm run dev:api already runs in this repo -
Node 24 strips TypeScript types natively, no build step needed for the API.)
For a dedicated production entry point (recommended over reusing the dev
server file directly), add a tiny wrapper that imports the same
createApiServer() export:
// api/_lib/prodServer.ts
import { createApiServer } from "./devServer.ts";
const port = Number(process.env.PORT ?? 3001);
createApiServer().listen(port, () => {
console.log(`commons API listening on :${port}`);
});
Run it as a persistent process with your process manager of choice (systemd, pm2, Docker's own restart policy, etc.) so it survives reboots and crashes:
# /etc/systemd/system/commons-api.service
[Unit]
Description=The Commons API (blob store)
After=network.target
[Service]
WorkingDirectory=/opt/commons
Environment=PORT=3001
Environment=BLOB_DB_PATH=/var/lib/commons/blobs.sqlite3
Environment=EVENT_DB_PATH=/var/lib/commons/events.sqlite3
ExecStart=/usr/bin/node api/_lib/prodServer.ts
Restart=always
User=commons
[Install]
WantedBy=multi-user.target
3. Serve the static frontend + proxy /api
Put a reverse proxy in front of both the static dist/ build and the API
process, so the whole app is reachable on a normal domain with TLS. Two
equivalent sketches:
Caddy (automatic TLS)
commons.example.com {
handle /api/* {
reverse_proxy 127.0.0.1:3001
}
handle {
root * /opt/commons/dist
try_files {path} /index.html
file_server
}
}
nginx
server {
listen 443 ssl;
server_name commons.example.com;
location /api/ {
proxy_pass http://127.0.0.1:3001;
proxy_set_header Host $host;
}
location / {
root /opt/commons/dist;
try_files $uri /index.html;
}
}
This mirrors vercel.json's own routing convention (/api/* goes to the
API, everything else falls back to index.html) so the same build works
unchanged whether it's proxied this way or (still) deployed to Vercel.
4. The BLOB_DB_PATH / EVENT_DB_PATH env vars and their persistent volume
-
Env vars:
BLOB_DB_PATH(mini-app bundles) andEVENT_DB_PATH(signed-event relay). Both behave identically. -
Defaults (if unset):
./.data/blobs.sqlite3and./.data/events.sqlite3, resolved under the process's own working directory (process.cwd()) - real files on disk, notos.tmpdir(). This matches the repo's existing.gitignoreconvention for local persistent state (the whole.data/directory is gitignored). -
On a self-hosted box, point it at a path that survives restarts and (if containerized) outlives the container filesystem - e.g.
/var/lib/commons/blobs.sqlite3on bare metal, or a bind-mounted host directory in Docker:docker run -d \ -e BLOB_DB_PATH=/data/blobs.sqlite3 \ -v /srv/commons-data:/data \ -p 3001:3001 \ commons-apiThe host directory
/srv/commons-datais what must persist; the container's own filesystem is disposable.
No external account or credential is required for this storage path.
The SQLite file is created automatically on first use
(CREATE TABLE IF NOT EXISTS) and needs nothing beyond a writable
directory on disk.
Vercel caveat (if you ever go that route instead)
vercel.json's /api/* file-routing convention still works unchanged if
you deploy this exact code to Vercel. But Vercel's serverless functions
only get a writable /tmp, which is ephemeral per-invocation, not a
persistent disk - a real Vercel deployment would need BLOB_DB_PATH
pointed at a genuinely persistent mounted volume, which Vercel's
serverless api/ functions do not provide. The supported path for
persistent blob storage is the long-running self-hosted process described
above (steps 1-4); don't assume the Vercel path "just works" for
persistence without that caveat.