# Cloudflare

The `cloudflare` preset deploys your app as a Cloudflare Worker. A production
build writes the Worker and its `wrangler.json`, with the bindings your
features need, under `.output/server`. Definitions and Runtime Helpers stay
host-neutral. Cloudflare details stay in the config and generated output.

| Feature | Default on the `cloudflare` preset |
| --- | --- |
| Blob | R2 bucket named after the app |
| KV | Workers KV binding `KV` |
| Database | D1 binding from the Database Definition |
| Queue | Cloudflare Queues with the app name as prefix |
| Rate Limit | Rate Limiting binding with the app name as namespace |
| Sandbox | Cloudflare Sandbox |
| Workflow | Cloudflare Workflows |
| Workspace | `memory` Store unless you select Cloudflare Artifacts |
| Browser and `email: true` | Browser Run and Cloudflare Email. Both require this preset. |

The app name comes from `vitehub({ name })`, then `WRANGLER_CI_OVERRIDE_NAME`
in Workers Builds, then the `package.json` name. It is also the Worker name
unless `nitro.cloudflare.wrangler.name` is set.

## Cloudflare boundaries

| Concern | ViteHub boundary |
| --- | --- |
| Workers and generated bundles | Provider Output (generated host files) from production builds. |
| D1, R2, KV, Queues, Browser Run, Rate Limiting bindings, Workflows, and Sandbox resources | Primitive package configuration, plus Provision Steps where available. |
| Credentials | Provider env vars for provisioning, or runtime secrets through Server Env. |
| Runtime context | Runtime Host Context from the host integration, not global app state. |
| Agent state | Agent state provider configuration when you select Cloudflare state. |

## Configure the preset

Select the `cloudflare` preset and turn on the features you use.

### Nuxt

```ts [nuxt.config.ts]
export default defineNuxtConfig({
  modules: ['vite-hub/nuxt'],
  vitehub: {
    preset: 'cloudflare',
    database: true,
    queue: true,
  },
})
```

### Nitro

```ts [vite.config.ts]
import { defineConfig } from 'vite'
import { nitro } from 'nitro/vite'
import { vitehub } from 'vite-hub'

export default defineConfig({
  plugins: [
    vitehub({
      preset: 'cloudflare',
      database: true,
      queue: true,
    }),
    nitro(),
  ],
})
```

### Vite

```ts [vite.config.ts]
import { defineConfig } from 'vite'
import { vitehub } from 'vite-hub'

export default defineConfig({
  plugins: [
    vitehub({
      preset: 'cloudflare',
      database: true,
      queue: true,
    }),
  ],
})
```

D1 metadata belongs to each Database Definition, because a Vite app can have several Named Databases.

```ts [src/database.ts] fragment
import { defineDatabase } from 'vite-hub/database'
import { notes } from './schema'

export default defineDatabase({
  cloudflare: {
    binding: 'DB',
    databaseId: '00000000-0000-0000-0000-000000000001',
    databaseName: 'app',
  },
  schema: { notes },
})
```

The Nuxt-only `database.driver: 'd1'` option sets up one host resource for Nuxt Content and Nitro. It is not a shortcut for a Vite Database Definition.

With the Console and Agents on, a Cloudflare build stores the Console run journal in this D1 binding. The journal creates its `vitehub_agent_invocations` table on first use. See [Cloudflare journal](https://vitehub.dev/docs/development/console#know-what-the-console-stores).

## Provision boundary

Provision shows a dry-run plan before it applies changes. A successful apply writes non-secret ids to `.vitehub/provision.json`. Secrets stay in environment variables or provider env stores.

```bash [Terminal]
CLOUDFLARE_ACCOUNT_ID=... CLOUDFLARE_API_TOKEN=... pnpm vitehub provision run --provider cloudflare --dry-run
CLOUDFLARE_ACCOUNT_ID=... CLOUDFLARE_API_TOKEN=... pnpm vitehub provision run --provider cloudflare
```

## Generated output

`.output/server` can hold Worker bundles, `wrangler.json`, D1 bindings, queue consumers, Rate Limiting bindings, cron triggers, and package runtime imports.

In Nuxt, the preset turns on Nitro's `cloudflare.deployConfig` by default, so a
local production build writes `.output/server/wrangler.json`.
`nitro.cloudflare.deployConfig: false` turns that file off.

```bash [Terminal]
pnpm build
test -f .output/server/wrangler.json
find .output -maxdepth 4 -type f | sort
```

### A project Wrangler config

A project `wrangler.json`, `wrangler.jsonc`, or `wrangler.toml` can declare bindings that ViteHub also writes, for example in a Deploy to Cloudflare template. Nitro merges that file into the generated config and joins the arrays. ViteHub then keeps one entry per name in `kv_namespaces`, `r2_buckets`, `ratelimits`, `hyperdrive`, `containers` (by matching `class_name` or `name`, with entries containing both linking the two forms), `durable_objects.bindings`, `migrations`, `queues`, `workflows`, `services`, `send_email`, and `analytics_engine_datasets`, at the top level and in each `env`. The Database integration does the same for `d1_databases`; see [Database hosts](https://vitehub.dev/docs/database/hosts). It also removes repeated `compatibility_flags` and `secrets.required` names.

- ViteHub's entry wins. The project entry fills fields it lacks, such as a KV `id`.
- An all-zero id, such as `00000000000000000000000000000000` or `00000000-0000-4000-8000-000000000000`, never names a resource. A real id from either side replaces it.
- For KV and D1, a primary id that stays all-zero is left out, so Wrangler provisions the resource on deploy.
- Unresolved `preview_id` and `preview_database_id` placeholders stay, so remote development cannot fall back to production. Replace them with real preview ids first.
- Hyperdrive needs an existing resource. Replace its placeholder with a real Hyperdrive id before you deploy.
- Two entries that name different resources fail the build with `INTERNAL_B0051`. Remove the project entry, or rename one.

Container duplicates must agree on `scheduling_policy`, which defaults to `default`. ViteHub rejects a policy change or conflicting image settings. It does not combine the default policy's `image` with Durable Object-managed `images`.

### Node.js compatibility in Nuxt

The Nuxt `cloudflare` preset turns on Nitro's `cloudflare.nodeCompat`, which adds `nodejs_compat` to `wrangler.json`. Nitro 2 also adds `no_nodejs_compat_v2` unless the config names `nodejs_compat_v2`, and that drops Node modules Workspaces import. ViteHub sets `nodejs_compat_v2` during the Nitro build, then swaps it back to `nodejs_compat` after the project Wrangler config is merged. Wrangler refuses both flags together.

When the app names `nodejs_compat_v2` or `no_nodejs_compat_v2` in `nitro.cloudflare.wrangler.compatibility_flags` or its project Wrangler config, ViteHub keeps the app's flags. `nodejs_compat_v2` alone gives `node:http` and `node:child_process` only from compatibility date 2026-08-04.

An explicit `no_nodejs_compat` in either config turns off Nitro's `cloudflare.nodeCompat` and skips ViteHub's rewrite, so the app opts out of Node.js compatibility completely.

Check the output before you deploy. `wrangler deploy --dry-run` validates the config. `wrangler dev` starts the Worker and catches a Node module that the date and flags leave out:

```bash [Terminal]
pnpm build
pnpm wrangler deploy --dry-run
pnpm wrangler dev
```

Agent routes come from Provider Output. Raw Worker fetch handlers are not a public Agent API.

A required secret Server Env declaration with one exact Env Source goes to `secrets.required` in the Wrangler config, and to each named Wrangler environment, since they do not inherit secrets. Wrangler reuses an existing Worker secret and stops the deploy when it is missing. ViteHub records only the name, never the value. Optional secrets, non-secret values, defaults, and fallback source lists stay runtime-only, because Wrangler's list cannot express fallbacks.

### Workers Builds

Use Nitro's generated deploy command for production and non-production Workers Builds:

```bash [Deploy command]
pnpm exec nitro deploy --prebuilt
```

With deploy config generation on (the default), ViteHub writes the command to `.output/nitro.json` as `wrangler deploy --config` with the generated `.output/server/wrangler.json`. It works from a copy of `.output` alone and inside a workspace with its own Wrangler config. Set `nitro.commands.deploy` to use your own. With `nitro.cloudflare.deployConfig: false`, ViteHub keeps Nitro's default command.

With config generation and Sandbox on, ViteHub writes an explicit gradual Container rollout to `.output/nitro.json`, so the Worker and its Container application deploy together.

> **Warning**

A direct Wrangler command with `--containers-rollout=none` skips ViteHub's deploy command. It can leave a new Sandbox binding without a Container application to run.

### Upload secrets from a file

A new Worker has no secrets yet, so `wrangler secret put` cannot set the values `secrets.required` asks for. To upload them when you deploy from your terminal, name a `.env` or JSON file:

### Nuxt

```ts [nuxt.config.ts]
export default defineNuxtConfig({
  modules: ['vite-hub/nuxt'],
  vitehub: {
    preset: 'cloudflare',
    cloudflare: { secretsFile: '.env' },
  },
})
```

### Nitro

```ts [vite.config.ts]
import { defineConfig } from 'vite'
import { nitro } from 'nitro/vite'
import { vitehub } from 'vite-hub'

export default defineConfig({
  plugins: [
    vitehub({
      preset: 'cloudflare',
      cloudflare: { secretsFile: '.env' },
    }),
    nitro(),
  ],
})
```

### Vite

```ts [vite.config.ts]
import { defineConfig } from 'vite'
import { vitehub } from 'vite-hub'

export default defineConfig({
  plugins: [
    vitehub({
      preset: 'cloudflare',
      cloudflare: { secretsFile: '.env' },
    }),
  ],
})
```

When the file exists at build time, ViteHub adds `--secrets-file` with its path to the deploy command in `.output/nitro.json`. `nitro deploy --prebuilt` then uploads every key in the file as a Worker secret.

- Wrangler keeps secrets that the file does not name.
- The path is relative to the project root. The command finds it from the project root or from `.output`.
- ViteHub only checks that the file exists. It never reads or copies the values.
- A build without the file, such as Workers Builds or Deploy to Cloudflare, writes the command without the flag. The secrets you set in the dashboard stay in use.
- The option needs Wrangler 4.74 or later. Your own `nitro.commands.deploy` replaces it, and it does nothing with `nitro.cloudflare.deployConfig: false`.

> **Warning**

Do not build with a local secrets file and deploy that `.output` from another machine.

### Rate Limiting bindings

With `vitehub({ preset: 'cloudflare', rateLimit: true })`, ViteHub selects the Cloudflare provider and uses the app name as the namespace. Each `requireRateLimit()` policy in a handler adds one `ratelimits` entry to Nitro's Wrangler config. Do not repeat those bindings in `nitro.cloudflare.wrangler`.

To use the Rate Limit integration directly, add the Nitro plugin with a Cloudflare preset and set the namespace yourself. The preset selects the Cloudflare provider, and Nitro writes the bindings to `wrangler.json`.

```ts [vite.config.ts]
import { hubRateLimit } from '@vite-hub/rate-limit/vite'
import { nitro } from 'nitro/vite'
import { defineConfig } from 'vite'

export default defineConfig({
  nitro: { preset: 'cloudflare-module' },
  plugins: [
    hubRateLimit({ namespace: 'acme-image-service-production' }),
    nitro(),
  ],
})
```

Cloudflare enforcement is best-effort, with 10-second or 60-second fixed windows. The build rejects `enforcement: 'strict'` and other periods rather than weaken a policy silently. Inspection reports per-location counters, unknown rejected-attempt behavior, and `availability: 'never'` for all quota metadata. The binding only allows or rejects.

The binding is request-scoped. If it is missing at runtime, the handle's `failure` policy decides whether to deny or allow the request. Rate Limiting bindings do not use Cloudflare KV and need no provisioning.

> **Warning**

Give each Worker and environment its own namespace. Cloudflare shares counters across all Workers in an account that use the same namespace id.

Workspace adds an Artifacts binding when its Store selects Cloudflare Artifacts:

### Nuxt

```ts [nuxt.config.ts]
export default defineNuxtConfig({
  modules: ['vite-hub/nuxt'],
  vitehub: {
    preset: 'cloudflare',
    workspace: {
      store: {
        provider: 'cloudflare-artifacts',
        binding: 'WORKSPACE_ARTIFACTS',
        namespace: 'vitehub',
      },
    },
  },
})
```

### Nitro

```ts [vite.config.ts]
import { defineConfig } from 'vite'
import { nitro } from 'nitro/vite'
import { vitehub } from 'vite-hub'

export default defineConfig({
  plugins: [
    vitehub({
      preset: 'cloudflare',
      workspace: {
        store: {
          provider: 'cloudflare-artifacts',
          binding: 'WORKSPACE_ARTIFACTS',
          namespace: 'vitehub',
        },
      },
    }),
    nitro(),
  ],
})
```

### Vite

```ts [vite.config.ts]
import { defineConfig } from 'vite'
import { vitehub } from 'vite-hub'

export default defineConfig({
  plugins: [
    vitehub({
      preset: 'cloudflare',
      workspace: {
        store: {
          provider: 'cloudflare-artifacts',
          binding: 'WORKSPACE_ARTIFACTS',
          namespace: 'vitehub',
        },
      },
    }),
  ],
})
```

Check the `artifacts` entry in `wrangler.json` before you deploy. Workspace keeps your other Artifacts bindings. Cloudflare still defaults to the ephemeral `memory` Store, because Artifacts needs beta access.

### Browser Run bindings

Register the Browser integration when trusted server code needs Browser Run sessions.

### Nuxt

```ts [nuxt.config.ts]
export default defineNuxtConfig({
  modules: ['vite-hub/nuxt'],
  vitehub: {
    preset: 'cloudflare',
    browser: true,
  },
})
```

### Nitro

```ts [vite.config.ts]
import { defineConfig } from 'vite'
import { nitro } from 'nitro/vite'
import { vitehub } from 'vite-hub'

export default defineConfig({
  plugins: [
    vitehub({
      preset: 'cloudflare',
      browser: true,
    }),
    nitro(),
  ],
})
```

### Vite

```ts [vite.config.ts]
import { defineConfig } from 'vite'
import { vitehub } from 'vite-hub'

export default defineConfig({
  plugins: [
    vitehub({
      preset: 'cloudflare',
      browser: true,
    }),
  ],
})
```

ViteHub writes the `browser` binding, a compatible default `compatibility_date`, and the `nodejs_compat` flag. Browser Definitions import helpers from `vite-hub/browser`. Do not import provider modules or the generated `wrangler.json`.

`wrangler dev` can run Browser Run against a local browser. Set `browser: { remote: true }` to run Worker code locally with the Browser binding on Cloudflare, for example to test the hosted service.

## Production notes

Local development and deployed Workers do not always behave the same. Use Provider Output Contracts and Local Provider Runs for pull request checks, and keep Live Smoke tests on real deployments small.

> **Warning**

The output can need real Worker bindings such as D1, R2, KV, Rate Limiting, Queues, Durable Objects, Cloudflare Artifacts, or Agent state. Check the bindings before you deploy, then smoke test the deployed Worker when they matter.

Agent Definitions run on Cloudflare through generated output when the Agent integration owns the route. Keep model keys, Durable Object state bindings, and other Runtime Env in Worker bindings.

### Console on Workers

Cloudflare can send two requests from one browser to different Workers. The [Console](https://vitehub.dev/docs/development/console) sends each operation as one stateless `POST /_vitehub/rpc/__call`, so it needs no session affinity or memory state. With session-based `auth`, the access policy checks the session cookie on each call. Agent runs started from the Console pass background work to the Worker's `waitUntil`. Configure a durable hosted run journal, as the [Console guide](https://vitehub.dev/docs/development/console) describes.

Protect a production Console with `console: { auth: { provider: 'cloudflare-access' } }` and a Cloudflare Access application. The Worker checks the Access token on each Console request with `CF_ACCESS_TEAM_DOMAIN` and `CF_ACCESS_AUD`. Inline GitHub Console Auth needs `node:sqlite`, so it does not work on Workers. See [Protect the Console route](https://vitehub.dev/docs/development/console#protect-the-console-route).

## Next steps

- Use [Runtime and host support](https://vitehub.dev/docs/frameworks-hosts/support-matrix) for exact package and proof coverage.
- Use [Provisioning](https://vitehub.dev/docs/development/provisioning) for resource creation.
- Use [Provider output](https://vitehub.dev/docs/reference/provider-output) for generated artifact families.
- Use [Verification](https://vitehub.dev/docs/development/verification) for Cloudflare proof tiers.

## Related guides

For worked tasks, see [Deploy an Agent](https://vitehub.dev/docs/guides/deploy-agent-cloudflare-vercel),
[D1 with Drizzle](https://vitehub.dev/docs/guides/cloudflare-d1),
[R2 file storage](https://vitehub.dev/docs/guides/cloudflare-r2), or
[Browser Run](https://vitehub.dev/docs/guides/cloudflare-browser).

<!-- vitehub-docs:section-links -->
## In this section

- [Choose a target](https://vitehub.dev/raw/docs/frameworks-hosts.md)
- [Frameworks](https://vitehub.dev/raw/docs/frameworks-hosts/nuxt.md)
- Deployment hosts
  - [Cloudflare](https://vitehub.dev/raw/docs/frameworks-hosts/cloudflare.md) (this page)
  - [Vercel](https://vitehub.dev/raw/docs/frameworks-hosts/vercel.md)
  - [Netlify](https://vitehub.dev/raw/docs/frameworks-hosts/netlify.md)
  - [Deno](https://vitehub.dev/raw/docs/frameworks-hosts/deno.md)
  - [Node and self-hosted](https://vitehub.dev/raw/docs/frameworks-hosts/node-self-hosted.md)
  - [Production deployment](https://vitehub.dev/raw/docs/frameworks-hosts/production.md)

Next: [Vercel](https://vitehub.dev/raw/docs/frameworks-hosts/vercel.md)

All pages: [llms.txt](https://vitehub.dev/llms.txt)
