Velocity CMS now runs on Cloudflare Workers, in 0.1.0-beta.27 and later. With the Cloudflare adapter, the whole CMS is one Worker: the editor, the API, sign-in and the live pages. The content lives in a D1 database and uploads in an R2 bucket, and both are reached by binding, so there is no database address and no storage key to keep.
Workers support is new, so it is marked experimental. This post covers what we changed and what we saw working on a deployed Worker.
Why Workers
Cloudflare has been on our list since the CMS went into beta. Uploads could already be kept in R2, through any S3-compatible bucket. The question we kept coming back to was the bigger one: could the whole thing run on Cloudflare?
Until now the answer was no. The CMS ran on Node on a server and on Vercel, with a libSQL database. Workers is a different runtime with no disk, no native libraries and a database that works differently. Supporting it meant changing how four things are done, without changing anything for sites on Node or Vercel.
What Is Different on Workers
The Database Is D1
Set the database address to d1: and the name of the binding, and the CMS uses the D1 database bound to the Worker. The Worker brings an empty database up to date by itself at its first request.
D1 cannot hold a transaction open while code decides what to write. So on D1 each save is sent as one batch that is kept whole or not at all. If what the save was decided from changed in the meantime, the batch is refused as a conflict, which the editor already knows how to handle.
Uploads Go to R2
Name an R2 binding and uploads are kept there. There are no access keys to create or rotate, because the Worker reaches the bucket directly.
Images Are Made Lighter by Cloudflare
Elsewhere the CMS uses sharp to turn each upload into a lighter WebP. Sharp is a native library and does not run in a Worker, so on Workers the same job is done by Cloudflare's Images binding, which the adapter adds for you.
Pages Are Kept in Cloudflare's Cache
Pages are kept in Cloudflare's cache, which every copy of the Worker shares, and a publish clears it. That is the same promise as on any other host: a published change is on the site at once, with no rebuild.
{
"compatibility_flags": ["nodejs_compat"],
"d1_databases": [{ "binding": "DB", "database_name": "my-site", "database_id": "<from wrangler d1 create>" }],
"r2_buckets": [{ "binding": "MEDIA", "bucket_name": "my-site-media" }],
"vars": {
"VELOCITY_CMS_DATABASE_URL": "d1:DB",
"VELOCITY_CMS_R2_BUCKET": "MEDIA",
"VELOCITY_CMS_ORIGIN": "https://example.com"
}
}That Wrangler config is most of the setup. The secret is set with npx wrangler secret put VELOCITY_CMS_SECRET, and npx velocity-cms init writes the two settings to .env for local development.
What We Tested on a Deployed Worker
We deployed the CMS starter to a workers.dev address with a real D1 database, a real R2 bucket and the Images binding, and checked it in a browser:
- The database. The Worker ran all 20 migrations on an empty D1 database at its first request, and pages drew from it.
- Sign-in and the editor. A one-time link signed the owner in, and the visual editor opened with the page beside the form.
- Publishing. The home page was being served from Cloudflare's cache. Right after a publish, the next request missed the cache and came back with the new words, and the one after that was cached again.
- Scheduled publishing. A change set to go out at a given time was live within half a minute of it.
- Uploads. A 244 KB JPEG was kept in R2 as an 82 KB WebP, with smaller copies served at 320, 640 and 1280 pixels wide.
- Forms. A post from the contact page arrived under Forms in the editor.
- Setup commands. The seed script and
velocity-cms ownerreached a real, empty D1 database from a laptop through Wrangler.
The Starter Kit Deploys There Too
Velocity 2.7.4, the starter kit, fixes its own Cloudflare build. Share images are drawn with sharp while the site builds, and the Cloudflare adapter was building pages inside the Workers runtime, where sharp cannot load. Pages are now built in Node, and only the form Actions run as a Worker.
We deployed a fresh project to check. Every page, the 404, the sitemap, a share image and both forms answered as they should.
pnpm add @astrojs/cloudflare
pnpm add -D wrangler
DEPLOY_TARGET=cloudflare pnpm build
pnpm wrangler deployNothing Changes on Node or Vercel
Sites on Node and Vercel run as before. One codebase serves every host: the parts that differ on Workers sit behind the same interfaces the rest of the CMS already used, and the tests every database adapter must pass also run against a stand-in for D1. If you are on Vercel, skip 0.1.0-beta.26 and go straight to beta.27: beta.26 left the database's migrations out of what Vercel ships, and beta.27 fixes it.
Try It
The steps are in the docs: Deploy on Cloudflare Workers. Velocity CMS is open source and MIT licensed. If something does not work on Workers, open an issue and we will look at it.