Self-Hosting Shaf on Cloudflare Workers
Organizations requiring 100% data sovereignty, custom compliance controls, or private network deployments can run Shaf on their own Cloudflare infrastructure. This guide provisions Cloudflare D1 and KV resources, runs database migrations, and deploys the Nitro edge Worker.
Prerequisites
- Node.js 22.x and pnpm 11.x installed on your deployment workstation.
- A Cloudflare account with the Wrangler CLI authenticated (
wrangler login). - A custom domain or free
workers.devsubdomain attached to your Cloudflare account.
Implementation Recipe
Clone the Repository and Install Dependencies
Clone the codebase and run the bootstrap sequence:
git clone https://github.com/shaf-is/shaf.git cd shaf pnpm installProvision Cloudflare D1 and KV Storage
Create your distributed SQLite database and Anycast KV cache using Wrangler:
# 1. Create D1 database wrangler d1 create shaf-db # 2. Create KV read-cache namespace wrangler kv namespace create shaf-kvNote the generated
database_idand KVidin the CLI output.Update Wrangler Configuration
In
wrangler.jsonc, insert the provisioned IDs into your storage bindings:{ "name": "shaf-core", "main": "./.output/server/index.mjs", "compatibility_date": "2025-02-14", "compatibility_flags": ["nodejs_compat"], "d1_databases": [ { "binding": "DB", "database_name": "shaf-db", "database_id": "YOUR_D1_DATABASE_ID" } ], "kv_namespaces": [ { "binding": "KV", "id": "YOUR_KV_NAMESPACE_ID" } ] }Apply D1 Database Migrations
Execute Drizzle ORM schema migrations against your remote D1 instance:
pnpm db:migrate:remoteExpected output:
Applying migration 0001_initial_schema.sql... Applying migration 0002_add_analytics_indices.sql... 100% migrated (all migrations applied successfully).Set Production Secrets
Configure mandatory security secrets via Wrangler:
# Generate cryptographic site secret wrangler secret put NUXT_SITE_TOKEN wrangler secret put SESSION_SECRETBuild and Deploy to Cloudflare Workers
Compile the client UI and Nitro server bundle, then deploy to Cloudflare:
pnpm build wrangler deployExpected output:
Uploaded shaf-core (2.45 MiB) Deployed to https://shaf-core.your-subdomain.workers.dev
Verification
Probe your self-hosted deployment to confirm Worker execution and D1 database connectivity:
-
Test the health status endpoint:
curl -s "https://shaf-core.your-subdomain.workers.dev/api/admin/system/health"Expected response (
200 OK):{ "status": "healthy", "database": "connected", "kv": "connected", "version": "1.0.0" } -
Inspect edge redirect execution:
curl -I "https://shaf-core.your-subdomain.workers.dev/docs"Expected header:
HTTP/2 301 Moved Permanently location: /docs/ server: cloudflare
Common Gotchas
| Issue / Error | Root Cause | Solution |
|---|---|---|
D1_ERROR: no such table: links |
Migrations were run locally but not deployed to the remote database. | Run pnpm db:migrate:remote to apply tables to the production D1 instance. |
KV_ERROR: namespace not found |
The id in wrangler.jsonc does not match the output of wrangler kv namespace create. |
Verify your KV IDs by running wrangler kv namespace list. |
Out of Memory during pnpm build |
The 8GB Node heap constraint was exceeded during full prerendering. | Increase Node memory limit: export NODE_OPTIONS="--max-old-space-size=8192". |
Missing NUXT_SITE_TOKEN error |
Mandatory authentication token secret was not set. | Run wrangler secret put NUXT_SITE_TOKEN and provide a random 32-character string. |