Deployment

Cloudflare

Run Tenon on Workers, with the CMS in a Durable Object.

On Cloudflare, your Astro site runs as a Worker, and the CMS runs in a Durable Object next to it. This page sets up the object, media, email and secrets, and deploys the site.

How it runs

The CMS lives in one Durable Object, with its own SQLite database. Every request to /_tenon and every read from a page goes to that object. Its alarm is also the job clock: it wakes when a job is due, with no cron trigger to set up.

Cloudflare works with Astro only, for now.

Before you start

  • A Cloudflare account, and Wrangler signed in: npx wrangler login.
  • R2 enabled in the Cloudflare dashboard. Without it, the first deploy fails with inherit binding 'MEDIA' is invalid.

The quickest start is npm create tenoncms@latest with Astro and the target Cloudflare. It writes every file below. The Worker is named after the project folder, and its bucket <name>-media, so two sites on one account stay apart.

Set it up

To add Tenon to an Astro site on Cloudflare yourself:

Install the packages

npm install tenoncms @astrojs/cloudflare
npm install -D wrangler

Configure Astro

Use the Cloudflare adapter, and render pages on the server:

astro.config.ts
import cloudflare from "@astrojs/cloudflare";
import { defineConfig, envField } from "astro/config";
import tenonCms from "tenoncms/astro";

export default defineConfig({
  output: "server",
  adapter: cloudflare(),
  // Tenon keeps its own sessions. Astro's sessions would need a KV namespace.
  session: false,
  integrations: [tenonCms()],
  env: {
    schema: {
      TENON_SECRET: envField.string({ context: "server", access: "secret", min: 32 }),
      TENON_SITE_URL: envField.string({
        context: "server",
        access: "secret",
        url: true,
        optional: true,
      }),
    },
  },
});

The integration detects the adapter and runs the CMS in the object.

Add the CMS object

durableObject() is the database. The Worker entry exports the object class, and wrangler.jsonc binds it as CMS:

import { TENON_SECRET, TENON_SITE_URL } from "astro:env/server";
import { env } from "cloudflare:workers";
import { defineCms } from "tenoncms";
import { durableObject } from "tenoncms/cloudflare";
import { cloudflare } from "tenoncms/media/cloudflare";
import { r2 } from "tenoncms/media/r2";

export default defineCms({
  database: durableObject(),
  secret: TENON_SECRET,
  site: { url: TENON_SITE_URL },
  media: {
    storage: r2(env.MEDIA),
    images: { provider: cloudflare({ binding: env.IMAGES }) },
  },
  // Your collections…
});
  • The binding MUST be named CMS. All content lives in one object.
  • Run npx wrangler types after a change to wrangler.jsonc. It writes the Env type of the bindings.

Set the secret for development

Put the secret in .dev.vars, which Wrangler reads in development. Keep the file out of git:

.dev.vars
TENON_SECRET=<the output of openssl rand -hex 32>

Start the site

Run npm run dev, open /admin and create the first admin. Development simulates the object, the bucket and the Images binding.

Read content

Astro.locals.tenon is the client over the object, not the local API. It sends the request's cookie to the object, so the signed-in user's access rules apply, and the preview cookie shows drafts:

On other targetsOn Cloudflare
withDrafts() and withSession()None. The cookies decide
create(…, { draft: true })createDraft()
cms.localizationawait localization()
cms.email and cms.auth()None
Any write from server codeWrites need a session or an API key

See Differences to the local API for the rest.

Pages that read the CMS MUST render per request. No object exists at build time, so a prerendered page that reads the CMS fails the build. Keep output: "server".

Media and images

  • r2(env.MEDIA) stores files through the bucket binding. It needs no keys and no CORS. Upload parts go through the Worker and the object.
  • cloudflare({ binding: env.IMAGES }) resizes images with Cloudflare Images.
  • An image service, like imgix, works too.

env comes from cloudflare:workers. See Media storage and Image providers.

Email

Cloudflare Email Service sends through a send_email binding, with no SMTP server:

tenon.config.ts
import { env } from "cloudflare:workers";
import { cloudflareEmail } from "tenoncms/email/cloudflare";

export default defineCms({
  // ...
  email: {
    adapter: cloudflareEmail(env.EMAIL),
    from: "My Site <hello@example.com>",
  },
});
wrangler.jsonc
"send_email": [{ "name": "EMAIL" }],
  • The sender's domain MUST be onboarded to Email Service in the Cloudflare dashboard.
  • wrangler dev only logs each mail. Add "remote": true to the binding to send real mail in development.
  • smtp() works on Workers too, on port 465 or 587.

See Cloudflare Email Service for its limits and errors.

Set up email before you go live. tenon users recover needs the database itself, so it cannot reach a Durable Object. Email is then the way back in after a lost password.

Deploy

  1. Set the secrets of the Worker:
    npx wrangler secret put TENON_SECRET
    npx wrangler secret put TENON_SITE_URL
    

    TENON_SITE_URL is the public origin, like https://example.com. Add the other variables of your config the same way, like EMAIL_FROM.
  2. Build and deploy:
    npm run deploy
    

    The starter's deploy script runs astro build && wrangler deploy. wrangler deploy creates the bucket of r2_buckets when it is missing.
  3. Open /admin on the deployed site and create the first admin.

The tenon CLI cannot load a Cloudflare config on your machine. Talk to the deployed site with --url:

npx tenon login --url https://example.com
npx tenon backup --url https://example.com

See Local or remote for which commands work that way.