cloudflare-workers
0
总安装量
6
周安装量
安装命令
npx skills add https://github.com/itechmeat/llm-code --skill cloudflare-workers
Agent 安装分布
opencode
5
claude-code
5
codex
5
cursor
5
gemini-cli
4
windsurf
4
Skill 文档
Cloudflare Workers
Serverless platform for building applications across Cloudflare’s global network.
Quick Navigation
- Runtime & handlers â
references/runtime.md - Bindings overview â
references/bindings.md - Wrangler configuration â
references/wrangler.md - Local development â
references/local-dev.md - Static assets â
references/assets.md - Testing â
references/testing.md
When to Use
- Building serverless APIs or full-stack applications
- Running edge functions with global distribution
- Connecting to Cloudflare storage (KV, R2, D1, Durable Objects)
- Running AI inference with Workers AI
- Configuring Wrangler CLI and wrangler.toml
- Setting up local development with Miniflare
- Testing Workers with Vitest
Module Worker (Recommended Syntax)
export default {
async fetch(request: Request, env: Env, ctx: ExecutionContext): Promise<Response> {
return new Response("Hello, World!");
},
} satisfies ExportedHandler<Env>;
Handler Types
| Handler | Trigger | Use Case |
|---|---|---|
fetch |
HTTP request | APIs, websites |
scheduled |
Cron trigger | Background jobs |
queue |
Queue message | Async processing |
email |
Email received | Email routing |
Bindings Overview
Access Cloudflare resources via env object.
| Binding | Type | Local Dev |
|---|---|---|
| KV | KVNamespace |
â Simulated |
| R2 | R2Bucket |
â Simulated |
| D1 | D1Database |
â Simulated |
| Durable Objects | DurableObjectNamespace |
â Simulated |
| Queues | Queue |
â Simulated |
| Workers AI | Ai |
â Remote only |
| Vectorize | VectorizeIndex |
â Remote only |
| Browser Rendering | Fetcher |
â Remote only |
| Hyperdrive | Hyperdrive |
â Remote only |
| Static Assets | Fetcher |
â Simulated |
| Service Binding | Fetcher |
â Simulated |
Minimal Configuration
// wrangler.jsonc
{
"$schema": "./node_modules/wrangler/config-schema.json",
"name": "my-worker",
"main": "src/index.ts",
"compatibility_date": "2025-03-07",
"compatibility_flags": ["nodejs_compat"],
"observability": {
"enabled": true,
"head_sampling_rate": 1
}
}
Essential Commands
# Development
npx wrangler dev # Local dev (Miniflare)
npx wrangler dev --remote # Remote dev (code on Cloudflare)
# Deployment
npx wrangler deploy # Deploy to production
npx wrangler versions upload # Upload without deploying
npx wrangler versions deploy # Promote version
# Secrets
npx wrangler secret put KEY # Add secret
npx wrangler secret list # List secrets
# Types
npx wrangler types # Generate TypeScript types
Compatibility Settings
# wrangler.toml
compatibility_date = "2025-03-07"
compatibility_flags = ["nodejs_compat"]
Key flags:
nodejs_compatâ Enable Node.js APIsnodejs_compat_v2â Improved process implementation
Quick Recipes
Fetch Handler with Routing
export default {
async fetch(request: Request, env: Env): Promise<Response> {
const url = new URL(request.url);
if (url.pathname === "/api/hello") {
return Response.json({ message: "Hello!" });
}
if (url.pathname.startsWith("/static/")) {
return env.ASSETS.fetch(request);
}
return new Response("Not Found", { status: 404 });
},
};
Scheduled Handler (Cron)
export default {
async scheduled(controller: ScheduledController, env: Env, ctx: ExecutionContext) {
ctx.waitUntil(doBackgroundWork(env));
},
};
# wrangler.toml
[triggers]
crons = ["0 * * * *"] # Every hour
Queue Consumer
export default {
async queue(batch: MessageBatch, env: Env, ctx: ExecutionContext) {
for (const message of batch.messages) {
console.log(message.body);
message.ack();
}
},
};
Critical Prohibitions
- Do NOT store secrets in
wrangler.tomlâ usewrangler secret put - Do NOT use
remote: truefor Durable Objects or Workflows â unsupported - Do NOT expect Workers AI to work locally â always requires remote connection
- Do NOT omit
compatibility_dateâ defaults to oldest date (2021-11-02) - Do NOT use Service Worker syntax for new projects â use ES Modules
- Do NOT mix Service Worker and Module syntax in same project
Common Gotchas
| Issue | Solution |
|---|---|
| Local bindings empty | Add --local flag to Wrangler KV/R2/D1 commands |
| AI not working locally | Use remote: true in ai binding config |
| Node APIs unavailable | Add nodejs_compat to compatibility_flags |
| First deploy fails | Use wrangler deploy, not versions upload for first deploy |
Related Skills
cloudflare-pagesâ Full-stack hosting with Git deploymentcloudflare-d1â D1 database operationscloudflare-r2â R2 object storagecloudflare-kvâ KV namespace operationscloudflare-durable-objectsâ Stateful coordination