Cloudflare workers edge functions

From CraftOS-dev/craftbot-agent-bundles · View interactive version

Cloudflare workers edge functions is an AI agent skill from the GitHub repository CraftOS-dev/craftbot-agent-bundles. Source: https://developers.cloudflare.com/workers/ · https://hono.dev/ · https://docs.partykit.io/

How to use this skill

Skill file

<!--

Source: https://developers.cloudflare.com/workers/ · https://hono.dev/ · https://docs.partykit.io/

Authored: June 2026 for the frontend-engineer agent bundle.

-->

Cloudflare Workers — Edge functions, Pages, Hono, PartyKit

Cloudflare Workers (V8 isolates) are the SOTA edge runtime in 2026 — sub-50ms

cold starts globally, generous free tier, KV/D1/R2/Durable Objects bundled.

Hono is the modern lightweight web framework that runs on Workers (and Vercel,

Bun, Deno, Node). PartyKit layers multiplayer/real-time on top of Durable Objects.

When to use

"PartyKit", "KV", "D1", "R2", "Durable Objects", "Cloudflare Pages"

Setup

# Wrangler (Cloudflare CLI) — global or per-project
pnpm add -D wrangler

# Authenticate (opens browser)
pnpm dlx wrangler login

# New Worker
pnpm dlx wrangler init my-worker --type=ts

# Hono Workers template
pnpm create hono@latest my-api -- --template=cloudflare-workers

# PartyKit template
pnpm dlx partykit@latest init my-party

Verify: pnpm dlx wrangler --version → 3.x.

Auth / API key requirements:

(free Workers tier: 100k req/day, no card required)

Common recipes

Recipe 1 — Minimal Worker

// src/index.ts
export default {
  async fetch(request: Request, env: Env, ctx: ExecutionContext): Promise<Response> {
    const url = new URL(request.url);
    if (url.pathname === "/health") return new Response("ok");
    return new Response("Hello from the edge", { headers: { "content-type": "text/plain" } });
  },
} satisfies ExportedHandler<Env>;
# wrangler.toml
name = "my-worker"
main = "src/index.ts"
compatibility_date = "2026-06-01"
compatibility_flags = ["nodejs_compat"]   # only if you need Node API shims

[observability]
enabled = true
pnpm dlx wrangler dev               # local dev on :8787 (Miniflare V3)
pnpm dlx wrangler deploy            # publish to *.workers.dev

Recipe 2 — Hono router with bindings

// src/index.ts
import { Hono } from "hono";
import { cors } from "hono/cors";
import { logger } from "hono/logger";

type Bindings = {
  DB: D1Database;
  CACHE: KVNamespace;
  BUCKET: R2Bucket;
};

const app = new Hono<{ Bindings: Bindings }>();
app.use("*", logger());
app.use("/api/*", cors({ origin: "https://example.com", credentials: true }));

app.get("/api/posts/:id", async (c) => {
  const id = c.req.param("id");
  const cached = await c.env.CACHE.get(`post:${id}`, "json");
  if (cached) return c.json(cached);

  const post = await c.env.DB.prepare("SELECT * FROM posts WHERE id = ?").bind(id).first();
  if (!post) return c.notFound();

  await c.env.CACHE.put(`post:${id}`, JSON.stringify(post), { expirationTtl: 300 });
  return c.json(post);
});

app.post("/api/posts", async (c) => {
  const body = await c.req.json<{ title: string; body: string }>();
  const result = await c.env.DB.prepare("INSERT INTO posts (title, body) VALUES (?, ?) RETURNING *")
    .bind(body.title, body.body)
    .first();
  return c.json(result, 201);
});

export default app;
# wrangler.toml
[[d1_databases]]
binding = "DB"
database_name = "my-app"
database_id = "abc-123-..."           # from `wrangler d1 create my-app`

[[kv_namespaces]]
binding = "CACHE"
id = "..."                            # from `wrangler kv namespace create CACHE`

[[r2_buckets]]
binding = "BUCKET"
bucket_name = "my-blobs"

Recipe 3 — D1 (SQLite at the edge)

pnpm dlx wrangler d1 create my-app
# Add the returned binding to wrangler.toml

# Apply schema
pnpm dlx wrangler d1 execute my-app --file=./schema.sql

# Local dev DB
pnpm dlx wrangler d1 execute my-app --local --file=./schema.sql

# Query
pnpm dlx wrangler d1 execute my-app --command="SELECT * FROM posts LIMIT 5"
-- schema.sql
CREATE TABLE IF NOT EXISTS posts (
  id INTEGER PRIMARY KEY AUTOINCREMENT,
  title TEXT NOT NULL,
  body TEXT NOT NULL,
  created_at TEXT DEFAULT CURRENT_TIMESTAMP
);
CREATE INDEX IF NOT EXISTS idx_posts_created ON posts(created_at DESC);

Recipe 4 — KV (key-value cache)

pnpm dlx wrangler kv namespace create CACHE
pnpm dlx wrangler kv namespace create CACHE --preview   # for `wrangler dev`
await c.env.CACHE.put("user:123", JSON.stringify(user), { expirationTtl: 3600 });
const cached = await c.env.CACHE.get("user:123", "json");
await c.env.CACHE.delete("user:123");

Recipe 5 — R2 (S3-compatible blob)

pnpm dlx wrangler r2 bucket create my-uploads
app.put("/api/upload/:key", async (c) => {
  const key = c.req.param("key");
  await c.env.BUCKET.put(key, c.req.raw.body!, {
    httpMetadata: { contentType: c.req.header("content-type") ?? "application/octet-stream" },
  });
  return c.json({ key });
});

app.get("/api/files/:key", async (c) => {
  const obj = await c.env.BUCKET.get(c.req.param("key"));
  if (!obj) return c.notFound();
  return new Response(obj.body, {
    headers: { "content-type": obj.httpMetadata?.contentType ?? "application/octet-stream" },
  });
});

Recipe 6 — Durable Object (single-instance, strongly consistent)

// src/counter.ts
export class Counter {
  state: DurableObjectState;
  constructor(state: DurableObjectState) { this.state = state; }

  async fetch(req: Request): Promise<Response> {
    let value = (await this.state.storage.get<number>("count")) ?? 0;
    const url = new URL(req.url);
    if (url.pathname === "/increment") value++;
    await this.state.storage.put("count", value);
    return new Response(JSON.stringify({ value }), { headers: { "content-type": "application/json" } });
  }
}
# wrangler.toml
[[durable_objects.bindings]]
name = "COUNTER"
class_name = "Counter"

[[migrations]]
tag = "v1"
new_classes = ["Counter"]
// In main worker
const id = env.COUNTER.idFromName(c.req.param("room"));
const stub = env.COUNTER.get(id);
return stub.fetch(new Request("https://internal/increment"));

Recipe 7 — Cloudflare Pages (deploy a static site)

pnpm build                            # produces ./dist or ./out
pnpm dlx wrangler pages deploy ./dist

Or wire to git:

pnpm dlx wrangler pages project create my-site
# Then connect via dash.cloudflare.com → Pages → connect to git

Recipe 8 — Next.js on Cloudflare Pages

pnpm add -D @cloudflare/next-on-pages
# Add to package.json scripts:
#   "build:cf": "next build && npx @cloudflare/next-on-pages"
pnpm build:cf
pnpm dlx wrangler pages deploy .vercel/output/static

Or for full SSR support: use @opennextjs/cloudflare.

Recipe 9 — PartyKit (multiplayer)

// src/server.ts
import type * as Party from "partykit/server";

export default class Chat implements Party.Server {
  constructor(readonly room: Party.Room) {}

  onConnect(conn: Party.Connection) {
    this.room.broadcast(JSON.stringify({ type: "joined", id: conn.id }), [conn.id]);
  }

  onMessage(message: string, sender: Party.Connection) {
    this.room.broadcast(JSON.stringify({ from: sender.id, text: message }));
  }
}
// In a React client
import usePartySocket from "partysocket/react";

const ws = usePartySocket({
  host: "my-party.alice.partykit.dev",
  room: "lobby",
  onMessage: (e) => setMessages((m) => [...m, JSON.parse(e.data)]),
});

ws.send("Hello!");
pnpm dlx partykit dev
pnpm dlx partykit deploy

Recipe 10 — Cron triggers

export default {
  async scheduled(event: ScheduledEvent, env: Env) {
    await env.CACHE.delete("homepage-data");
    // ... regenerate
  },
  async fetch(req, env) { /* ... */ },
} satisfies ExportedHandler<Env>;
# wrangler.toml
[triggers]
crons = ["0 * * * *"]                # every hour

Recipe 11 — Local dev with real bindings

pnpm dlx wrangler dev --local --persist-to=.wrangler/state

--local runs Miniflare locally with real KV/D1/R2 emulation; --persist-to

keeps state between restarts.

Examples

Example 1: API at the edge, frontend on Vercel

  1. pnpm create hono@latest my-api --template cloudflare-workers
  2. Add D1 binding (Recipe 3)
  3. pnpm dlx wrangler deploy → https://my-api.alice.workers.dev
  4. Set NEXT_PUBLIC_API_URL=https://my-api.alice.workers.dev on Vercel
  5. Frontend fetches the edge API; sub-50ms p50 globally

Example 2: Multiplayer game with PartyKit

  1. pnpm dlx partykit init game → scaffolds server + client
  2. Implement onMessage (Recipe 9)
  3. React client uses usePartySocket hook
  4. pnpm dlx partykit deploy — provisions a Durable Object globally

Edge cases / gotchas

a Durable Object alarm or a queue.

wrangler.toml if libraries depend on Node APIs.

transactions. Plan for eventual consistency on replicas.

region that wrote are immediate; other regions may lag.

shared global counter (use D1 + atomic update instead).

wrangler secret put SECRET_NAME for secrets.

set --persist-to or test with fresh runs.

some Server Actions / middleware quirks. Test thoroughly before committing.

message — Durable Objects bill separately.

Sources

Use this skill with your AI agent: Open on Skill Issue to copy, download, or add to your library. Works with Claude, ChatGPT, Gemini, Cursor, Windsurf and more.