Skip to content

Deploy to Cloudflare

Run Bookhoarder as a Cloudflare Worker, backed by a native R2 binding.

Updated View as Markdown

Bookhoarder can run as a Cloudflare Worker instead of a container, using the OpenNext Cloudflare adapter to build the Next.js app for the Workers runtime. This path doesn’t use Docker at all — you’re deploying from a checkout of the app’s source.

Create an R2 bucket

In the Cloudflare dashboard, under R2 Object Storage.

Set the binding in wrangler.jsonc

Point r2_buckets at the bucket, and set STORAGE_DRIVER: r2 under vars.

Build and deploy

pnpm deploy:cloudflare builds the app and pushes the Worker.

Prerequisites

  • A Cloudflare account, with Wrangler installed and authenticated (wrangler login)
  • A clone of the Bookhoarder repo — this deploys from source, not the published container image

Configure wrangler.jsonc

The repo’s wrangler.jsonc declares the R2 binding the storage driver reads at runtime:

wrangler.jsoncjsonc
{
  "main": "./.open-next/worker.js",
  "name": "bookhoard",
  "compatibility_date": "2024-12-30",
  "compatibility_flags": ["nodejs_compat", "global_fetch_strictly_public"],
  "assets": {
    "directory": ".open-next/assets",
    "binding": "ASSETS"
  },
  "vars": {
    "STORAGE_DRIVER": "r2"
  },
  "r2_buckets": [
    {
      "binding": "BOOKHOARD_BUCKET",
      "bucket_name": "bookhoard"
    }
  ]
}

Create an R2 bucket

In the Cloudflare dashboard, go to R2 Object Storage and create a bucket. Use its name for bucket_name above.

Pick a Worker name

Set name to whatever you want the Worker (and its default *.workers.dev URL) to be called.

Set the binding name

binding (BOOKHOARD_BUCKET by default) must match R2_BUCKET_BINDING — leave both alone unless you have a reason to rename it.

Build and deploy

pnpm install
pnpm deploy:cloudflare

deploy:cloudflare runs the Cloudflare-specific build (scripts/build-cloudflare.sh, which builds with opennextjs-cloudflare and keeps your local .env out of the bundle) and then opennextjs-cloudflare deploy. To try it locally in the Workers runtime first, without deploying:

pnpm preview:cloudflare

Other environment variables

STORAGE_DRIVER and the R2 binding come from wrangler.jsonc. For anything else — SMTP_URL, OPENLIBRARY_CONTACT — add plain values under vars in wrangler.jsonc, or use Wrangler secrets for anything sensitive:

wrangler secret put SMTP_URL

Custom domains

A Worker deploys to <name>.<subdomain>.workers.dev by default. To serve it from your own domain, add a custom domain or route in the Cloudflare dashboard under the Worker’s Settings.

Navigation

Type to search…

↑↓ navigate↵ selectEsc close