| name | cloudflare-workers |
| description | Use when developing and deploying Cloudflare Workers, implementing edge computing logic, optimizing performance, and configuring global distribution. Includes Workers API, KV storage, Durable Objects, and performance optimization. Based on Cloudflare Certified Developer and edge computing best practices. |
| license | MIT |
Cloudflare Workers Skill — Edge Computing & Global Distribution
Overview
This skill provides comprehensive guidance for developing and deploying Cloudflare Workers, implementing edge computing logic, optimizing performance, and configuring global distribution. It includes Workers API, KV storage, Durable Objects, and performance optimization. Based on Cloudflare Certified Developer and edge computing best practices.
When to Use
Use this skill when:
- Developing Cloudflare Workers for edge computing
- Implementing edge computing logic (authentication, routing, caching)
- Optimizing performance and cold starts for Workers
- Configuring global distribution with Cloudflare's edge network
- Using KV storage for fast read operations
- Using Durable Objects for stateful operations
- Using D1 database for structured data
- Implementing authentication and JWT validation at edge
- Implementing rate limiting and bot protection
- Setting up edge caching strategies
- Creating middleware for request/response modification
- Implementing A/B testing and feature flags
- Using Cloudflare Analytics and monitoring
- Configuring Workers with Wrangler CLI
- Setting up local development environment
- Deploying Workers with Git integration
- Using Cloudflare Queues for async processing
- Implementing WebSockets at the edge
- Using R2 storage for edge data
Do NOT use this skill when:
- Writing application business logic (use developer skill or specific framework skill)
- Designing database schema (use database-design skill)
- Creating UI components (use frontend-design skill)
- Managing non-Cloudflare infrastructure (use aws-serverless, azure-cloud, or gcp-cloud skills)
- Performing security audits (use security-auditor skill)
- Setting up Kubernetes deployments (use devops-pipeline skill)
- Deploying to Cloudflare Pages (use cloudflare-pages skill - different use case)
- Deploying to Vercel (use vercel-deployment skill)
Why avoid: Cloudflare Workers is for full edge computing. For Pages deployment or other platforms, use targeted skills.
Workers Architecture
Basic Worker
export default {
async fetch(request, env, ctx) {
return new Response('Hello World!');
},
};
Edge Function
export default {
async fetch(
request: Request,
env: Env,
ctx: ExecutionContext
): Promise<Response> {
const url = new URL(request.url);
if (url.pathname === '/api/data') {
return handleData(request, env);
}
return new Response('Not Found', { status: 404 });
},
};
async function handleData(request: Request, env: Env): Promise<Response> {
return new Response(JSON.stringify({ data: 'value' }));
}
Performance Optimization
Cold Start Optimization
let cachedData = null;
export default {
async fetch(request, env, ctx) {
if (!cachedData) {
cachedData = await env.DB.prepare('SELECT * FROM data').all();
}
return new Response(JSON.stringify(cachedData));
},
};
Minimal Dependencies
export default {
async fetch(request, env, ctx) {
const result = processRequest(request);
return new Response(JSON.stringify(result));
},
};
function processRequest(request) {
return { processed: true };
}
Streaming
export default {
async fetch(request, env, ctx) {
const stream = new ReadableStream({
async start(controller) {
controller.enqueue('data');
controller.close();
},
});
return new Response(stream, {
headers: { 'Content-Type': 'text/event-stream' },
});
},
};
Storage Options
KV Storage
export default {
async fetch(request, env, ctx) {
const { KV } = env;
const value = await KV.get('key');
return new Response(value);
},
};
Durable Objects
export default {
async fetch(request, env, ctx) {
const { DO } = env;
const id = env.DO.idFromName('my-object');
const stub = env.DO.get(id);
const response = await stub.fetch(request);
return response;
},
};
D1 Database
export default {
async fetch(request, env, ctx) {
const { D1 } = env;
const result = await D1.prepare('SELECT * FROM users').all();
return new Response(JSON.stringify(result));
},
};
Authentication at Edge
JWT Validation
export default {
async fetch(request, env, ctx) {
const authHeader = request.headers.get('Authorization');
if (!authHeader) {
return new Response('Unauthorized', { status: 401 });
}
const token = authHeader.replace('Bearer ', '');
try {
const payload = await validateJWT(token, env.JWT_SECRET);
request.headers.set('X-User-ID', payload.userId);
return fetch(request);
} catch (error) {
return new Response('Unauthorized', { status: 401 });
}
},
};
async function validateJWT(token, secret) {
}
Session Management
export default {
async fetch(request, env, ctx) {
const session = await getSession(request, env);
if (!session && request.url.includes('/api')) {
return new Response('Unauthorized', { status: 401 });
}
return fetch(request);
},
};
async function getSession(request, env) {
const cookie = request.headers.get('Cookie');
const sessionId = parseCookie(cookie, 'session_id');
if (!sessionId) {
return null;
}
return await env.KV.get(`session:${sessionId}`);
}
Routing Strategies
Path-based Routing
export default {
async fetch(request, env, ctx) {
const url = new URL(request.url);
switch (url.pathname) {
case '/api/users':
return handleUsers(request, env);
case '/api/products':
return handleProducts(request, env);
case '/api/orders':
return handleOrders(request, env);
default:
return new Response('Not Found', { status: 404 });
}
},
};
async function handleUsers(request, env) {
}
async function handleProducts(request, env) {
}
async function handleOrders(request, env) {
}
Method-based Routing
export default {
async fetch(request, env, ctx) {
const url = new URL(request.url);
if (url.pathname === '/api/data') {
switch (request.method) {
case 'GET':
return getData(request, env);
case 'POST':
return createData(request, env);
case 'PUT':
return updateData(request, env);
case 'DELETE':
return deleteData(request, env);
default:
return new Response('Method Not Allowed', { status: 405 });
}
}
return new Response('Not Found', { status: 404 });
},
};
async function getData(request, env) {
}
async function createData(request, env) {
}
async function updateData(request, env) {
}
async function deleteData(request, env) {
}
Common Anti-Patterns
❌ Bad: Large Dependencies
import { bigLibrary } from 'big-library';
export default {
async fetch(request, env, ctx) {
const result = bigLibrary.process(request);
return new Response(JSON.stringify(result));
},
};
Problems:
- Slow cold starts
- High memory usage
- Expensive execution
✅ Good: Minimal Dependencies
export default {
async fetch(request, env, ctx) {
const result = processRequest(request);
return new Response(JSON.stringify(result));
},
};
function processRequest(request) {
return { processed: true };
}
❌ Bad: No Caching
export default {
async fetch(request, env, ctx) {
const response = await fetch(request);
return response;
},
};
Problems:
- High latency
- Origin server load
- No global distribution
✅ Good: With Caching
export default {
async fetch(request, env, ctx) {
const cached = await caches.default.match(request);
if (cached) {
return cached;
}
const response = await fetch(request);
ctx.waitUntil(caches.default.put(request, response.clone()));
return response;
},
};
Real-World Impact
Before this skill:
- Slow cold starts
- High latency
- Origin server load
- No global distribution
After this skill:
- Fast cold starts
- Low latency
- Edge processing
- Global distribution
Cross-References
cloudflare-pages - For Pages deployment
devops-pipeline - For CI/CD integration
testing-strategy - For testing edge functions
References