Ship It: Runtime Mode, Cloudflare, and a Smoke Test
Build Ampyard for Cloudflare Workers, create the D1 database (and optional KV namespace), run the schema task, deploy, and prove the depot answers with nitro serve and nitro fetch.
The depot works locally. Stage 9 puts it on the internet: one build, a real D1 database, an optional KV namespace, Cron Triggers, a Durable Object for the radio, and a smoke test from the CLI.
What it is#
Nitro v3 can build for Cloudflare Workers with the cloudflare_module preset:
export default defineConfig({
preset: "cloudflare_module",
compatibilityDate: "2026-11-01",
cloudflare: {
wrangler: {
name: "ampyard",
// Optional: only needed for the KV lesson path.
kv_namespaces: [{ binding: "AMPYARD_KV", id: "..." }],
d1_databases: [
{ binding: "DB", database_name: "ampyard", database_id: "..." },
],
durable_objects: {
bindings: [{ name: "$DurableObject", class_name: "$DurableObject" }],
},
exports: {
$DurableObject: { type: "durable-object", storage: "sqlite" },
},
},
},
});
The build produces one .output/ directory:
.output/
├─ public/ # client assets
└─ server/
├─ index.mjs # Worker entry
└─ wrangler.json # generated bindings + crons
Nitro writes the Wrangler config for you. The cloudflare.wrangler object is merged with the generated values, so the cron triggers from scheduledTasks appear without a manual wrangler.json.
Why you'd care#
The point of Nitro is that the same source runs on more than one runtime. Ampyard uses that:
- Local dev runs in Nitro's Vite dev server with Miniflare emulating D1 (and optional KV), cron, and the Durable Object.
- Runtime mode runs the sources without a build:
nitro serve. - Production runs the built Worker on Cloudflare.
The app code does not branch on the platform. The bindings are the platform seam.
What we're building#
A deploy checklist:
- Create a D1 database. Create a KV namespace only if you want the optional KV path.
- Put the real IDs into
nitro.config.ts. - Build and deploy.
- Run the schema task against production.
- Smoke-test the deployed depot.
And a second way to run the same code:
nitro serve
nitro fetch /api/depot
Before#
Stage 8 passes locally. The D1 and optional KV bindings in nitro.config.ts are placeholders, and the Worker has never been deployed.
After#
Create the resources:
npx wrangler d1 create ampyard
# Optional: only if you want the KV lesson path.
npx wrangler kv namespace create AMPYARD_KV
Wrangler prints the IDs. Put them into nitro.config.ts:
// Optional: only for AMPYARD_STATE_STORE=kv.
kv_namespaces: [
{ binding: "AMPYARD_KV", id: "a1b2c3..." },
],
d1_databases: [
{ binding: "DB", database_name: "ampyard", database_id: "8f7e..." },
],
Build:
pnpm --filter ampyard build
Deploy the prebuilt output:
npx nitro deploy --prebuilt
nitro deploy reads the generated Wrangler config, runs wrangler deploy, and uploads the Worker, the assets, the cron triggers, and the Durable Object export.
Run the schema against production. The first request would create the tables lazily, but a deploy step is more honest:
curl -s -X POST https://ampyard.example.workers.dev/api/ops/tick | jq '.tick'
That route calls depot:simulate, which calls setupDatabase(). The tables exist before the first driver arrives.
Smoke test:
curl -s https://ampyard.example.workers.dev/api/depot | jq '.summary'
curl -s https://ampyard.example.workers.dev/api/tariffs | jq '.[0]'
Open the Scalar docs:
https://ampyard.example.workers.dev/_scalar
Runtime mode#
Nitro v3 can also run without a build:
nitro serve
It starts a production server from your sources, with WebSockets, scheduled tasks, public assets, .env loading, and graceful shutdown. The sources run in the main process, or in a worker when build plugins are involved. TypeScript, JSX, aliases, and import.meta.* work the same as a bundled build.
nitro serve --dev watches the sources and restarts on change. To make nitro dev use runtime mode too:
export default defineConfig({
builder: false,
});
nitro fetch sends one request to the app from source, prints the response, and exits:
nitro fetch /api/depot | jq '.summary'
nitro fetch -i -X POST /api/ops/tick
When vite dev is running, nitro fetch sends the request to the dev server. Otherwise it starts a runtime server, sends the request, and closes it. The body streams to stdout and the logs go to stderr, so it pipes.
Runtime mode is experimental. Bundler options and build plugins do not apply. Treat it as a fast smoke test and a local production preview, not a replacement for the build in CI.
The live stage#
The preview above is /preview/9. It is the full depot: live board, holds, queue, ledger, clock, SSE, radio, OpenAPI, and the ship panel. The stage rail still works, so a reader can jump back to any earlier stage and see the app without the later features.
Gotchas#
- Replace the placeholder IDs. The local Miniflare bindings accept placeholders. Wrangler deploy does not.
- D1 schema is not a migration system.
CREATE TABLE IF NOT EXISTSis idempotent, but it does not alter an existing table. A real deploy needs versioned migrations. - KV is eventually consistent. The live state and ledger are both D1 by default. If you opt into
AMPYARD_STATE_STORE=kv, a hold can appear a moment after a write in another region. - The Durable Object binding is required for pub/sub. Without it, the ops radio works in a single connection and silently fails to fan out.
- Cron Triggers are UTC. A "daily" rollup at
0 0 * * *is midnight UTC, not midnight in the depot's timezone. production: "runtime"exposes the docs. Protect them with auth if the depot is real.- RC churn. Pin
nitroandwrangler. Re-read the generatedwrangler.jsonafter upgrading; the shape can change. - Cold starts. The first request after a quiet period runs the schema promise and opens the D1 connection. Warm it with a health check or a scheduled task.
Recap#
Ampyard is shipped: one build, Cloudflare D1 (and an optional KV path), Cron Triggers, a Durable Object, OpenAPI docs, and a runtime-mode smoke test. The same source ran on the Vite dev server, in nitro serve, and on Workers.
That is the series. You built a server, not a slide deck: web-standard routes, a cache with a spine, D1 holds, a durable ledger, a clock, two realtime transports, a machine contract, and a deploy. Take the stage route with you. It is the most honest tutorial infrastructure I know.
← Back to the series: Nitro v3 in Practice: Build Ampyard.