All skills
openai avatar

/cloudflare-deploy

@bf9e226 official
by openaiopenai/skills28k stars
1,891

Deploy applications and infrastructure to Cloudflare using Workers, Pages, and related platform services. Use when the user asks to deploy, host, publish, or set up a project on Cloudflare.

Use this Skill: https://skilld.dev/gh/openai/skills/cloudflare-deploy

This session only. Nothing lands on disk.

referencesdurable-objectsgotchas.md

≈1.8k tokens on demand. Your agent reads this file only when SKILL.md points to it.

Durable Objects Gotchas

Common Errors

"Hibernation Cleared My In-Memory State"

Problem: Variables lost after hibernation
Cause: DO auto-hibernates when idle; in-memory state not persisted
Solution: Use ctx.storage for critical data, ws.serializeAttachment() for per-connection metadata

// ❌ Wrong - lost on hibernation
private userCount = 0;
async webSocketMessage(ws: WebSocket, msg: string) {
  this.userCount++;  // Lost!
}

// ✅ Right - persisted
async webSocketMessage(ws: WebSocket, msg: string) {
  const count = this.ctx.storage.kv.get("userCount") || 0;
  this.ctx.storage.kv.put("userCount", count + 1);
}

"setTimeout Didn't Fire After Restart"

Problem: Scheduled work lost on eviction
Cause: setTimeout in-memory only; eviction clears timers
Solution: Use ctx.storage.setAlarm() for reliable scheduling

// ❌ Wrong - lost on eviction
setTimeout(() => this.cleanup(), 3600000);

// ✅ Right - survives eviction
await this.ctx.storage.setAlarm(Date.now() + 3600000);
async alarm() { await this.cleanup(); }

"Constructor Runs on Every Wake"

Problem: Expensive init logic slows all requests
Cause: Constructor runs on every wake (first request after eviction OR after hibernation)
Solution: Lazy initialization or cache in storage

Critical understanding: Constructor runs in two scenarios:

  1. Cold start - DO evicted from memory, first request creates new instance
  2. Wake from hibernation - DO with WebSockets hibernated, message/alarm wakes it
// ❌ Wrong - expensive on every wake
constructor(ctx: DurableObjectState, env: Env) {
  super(ctx, env);
  this.heavyData = this.loadExpensiveData();  // Slow!
}

// ✅ Right - lazy load
private heavyData?: HeavyData;
private getHeavyData() {
  if (!this.heavyData) this.heavyData = this.loadExpensiveData();
  return this.heavyData;
}

"Durable Object Overloaded (503 errors)"

Problem: 503 errors under load
Cause: Single DO exceeding ~1K req/s throughput limit
Solution: Shard across multiple DOs (see Patterns: Sharding)

"Storage Quota Exceeded (Write failures)"

Problem: Write operations failing
Cause: DO storage exceeding 10GB limit or account quota
Solution: Cleanup with alarms, use deleteAll() for old data, upgrade plan

"CPU Time Exceeded (Terminated)"

Problem: Request terminated mid-execution
Cause: Processing exceeding 30s CPU time default limit
Solution: Increase limits.cpu_ms in wrangler.jsonc (max 300s) or chunk work

"WebSockets Disconnect on Eviction"

Problem: Connections drop unexpectedly
Cause: DO evicted from memory without hibernation API
Solution: Use WebSocket hibernation handlers + client reconnection logic

"Migration Failed (Deploy error)"

Cause: Non-unique tags, non-sequential tags, or invalid class names in migration
Solution: Check tag uniqueness/sequential ordering and verify class names are correct

"RPC Method Not Found"

Cause: compatibility_date < 2024-04-03 preventing RPC usage
Solution: Update compatibility_date to >= 2024-04-03 or use fetch() instead of RPC

"Only One Alarm Allowed"

Cause: Need multiple scheduled tasks but only one alarm supported per DO
Solution: Use event queue pattern to schedule multiple tasks with single alarm

"Race Condition Despite Single-Threading"

Problem: Concurrent requests see inconsistent state
Cause: Async operations allow request interleaving (await = yield point)
Solution: Use blockConcurrencyWhile() for critical sections or atomic storage ops

// ❌ Wrong - race condition
async incrementCounter() {
  const count = await this.ctx.storage.get("count") || 0;
  // ⚠️ Another request could execute here during await
  await this.ctx.storage.put("count", count + 1);
}

// ✅ Right - atomic operation
async incrementCounter() {
  return this.ctx.storage.sql.exec(
    "INSERT INTO counters (id, value) VALUES (1, 1) ON CONFLICT(id) DO UPDATE SET value = value + 1 RETURNING value"
  ).one().value;
}

// ✅ Right - explicit locking
async criticalOperation() {
  await this.ctx.blockConcurrencyWhile(async () => {
    const count = await this.ctx.storage.get("count") || 0;
    await this.ctx.storage.put("count", count + 1);
  });
}

"Migration Rollback Not Supported"

Cause: Attempting to rollback a migration after deployment
Solution: Test with --dry-run before deploying; migrations cannot be rolled back

"deleted_classes Destroys Data"

Problem: Migration deleted all data
Cause: deleted_classes migration immediately destroys all DO instances and data
Solution: Test with --dry-run; use transferred_classes to preserve data during moves

"Cold Starts Are Slow"

Problem: First request after eviction takes longer
Cause: DO constructor + initial storage access on cold start
Solution: Expected behavior; optimize constructor, use connection pooling in clients, consider warming strategy for critical DOs

// Warming strategy (periodically ping critical DOs)
export default {
  async scheduled(event: ScheduledEvent, env: Env) {
    const criticalIds = ["auth", "sessions", "locks"];
    await Promise.all(criticalIds.map(name => {
      const id = env.MY_DO.idFromName(name);
      const stub = env.MY_DO.get(id);
      return stub.ping();  // Keep warm
    }));
  }
};

Limits

Limit Free Paid Notes
SQLite storage per DO 10 GB 10 GB Per Durable Object instance
SQLite total storage 5 GB Unlimited Account-wide quota
Key+value size 2 MB 2 MB Single KV pair (SQLite/async)
CPU time default 30s 30s Per request; configurable
CPU time max 300s 300s Set via limits.cpu_ms
DO classes 100 500 Distinct DO class definitions
SQL columns 100 100 Per table
SQL statement size 100 KB 100 KB Max SQL query size
WebSocket message size 32 MiB 32 MiB Per message
Request throughput ~1K req/s ~1K req/s Per DO (soft limit - shard for more)
Alarms per DO 1 1 Use queue pattern for multiple events
Total DOs Unlimited Unlimited Create as many instances as needed
WebSockets Unlimited Unlimited Within 128MB memory limit per DO
Memory per DO 128 MB 128 MB In-memory state + WebSocket buffers

Hibernation Caveats

  1. Memory cleared - All in-memory variables lost; reconstruct from storage or deserializeAttachment()
  2. Constructor reruns - Runs on wake; avoid expensive operations, use lazy initialization
  3. No guarantees - DO may evict instead of hibernate; design for both
  4. Attachment limit - serializeAttachment() data must be JSON-serializable, keep small
  5. Alarm wakes DO - Alarm prevents hibernation until handler completes
  6. WebSocket state not automatic - Must explicitly persist with serializeAttachment() or storage

See Also

Source: SKILL.md on GitHub

2 warnings17d5 checks · Risk SAFE
  • Gen Agent Trust Hub17d

    This skill provides comprehensive guidance for deploying and managing infrastructure on the Cloudflare platform. It includes extensive educational material on secure development practices, such as preventing SQL injection and managing secrets effectively. No malicious patterns or security risks were identified.

  • Socket17d

    2 alerts: gptAnomaly

  • Snyk17d

    Risk: LOW · No issues

  • Runlayer7mo

    310/310 files flagged

  • ZeroLeaks5mo

    Score: 93/100 · 2 sections analyzed

Signed by skilld at bf9e226. This ties the file your Agent reads to that commit on GitHub. It does not review the instructions.

Last checked against GitHub 2 months ago.

Activeupdated 8 months ago

README badge

README badge for openai/skills/cloudflare-deploy

Deploys applications and infrastructure to Cloudflare's platform, including Workers, Pages, D1, R2, Durable Objects, KV, and other services. Use decision trees to route to the right Cloudflare product based on compute, storage, AI, networking, security, or media needs.

Generated from the current SKILL.md.

Does this skill cover all Cloudflare products?
The skill is a consolidated index covering compute, storage, AI, networking, security, media, and developer tools on Cloudflare. It uses decision trees to route you to the right product reference, then loads detailed guidance for that product.
What authentication is required before deploying?
Run `npx wrangler whoami` to check if authenticated. For local deployment, use `wrangler login` (one-time OAuth). For CI/CD, set the `CLOUDFLARE_API_TOKEN` environment variable.
What should I do if deployment fails due to network issues?
Rerun the deploy with `sandbox_permissions=require_escalated` to grant elevated network access, which is required for outbound requests to Cloudflare during deployment.
How long does a Cloudflare deployment typically take?
Deployments may take several minutes. Use appropriate timeout values in your configuration or CI/CD environment.

Generated from the current SKILL.md. These answers refresh after source changes.