cfw-api-startup-optimization

Diagnose and optimize Cloudflare Worker startup CPU time for the cfw-api service.

7|12|Updated Mar 26, 2026
One-click install
npx skills add https://github.com/OpenRouterTeam/docs --skill cfw-api-startup-optimization-openrouterteam
Or copy as Structured Prompt for Agent▼
Please help me install this Agent Skill.
Skill: cfw-api-startup-optimization
Source: https://github.com/OpenRouterTeam/docs/tree/main/.agents/skills/cfw-api-startup-optimization
Command: npx skills add https://github.com/OpenRouterTeam/docs --skill cfw-api-startup-optimization-openrouterteam

SYSTEM DOCUMENTATION & REQUIREMENTS

What problem does it solve? Cloudflare Workers enforce a 1-second CPU time limit during script startup, and cfw-api deploys are rejected with error 10021 when the bundle exceeds it. This Skill provides the measurement methodology, historical record of tried optimizations, and current architecture context needed to investigate startup failures without repeating failed approaches. ## Core Features & Use Cases - Statistically Rigorous Measurement: Trigger the upload timing workflow with serial alternating A/B blocks and sample sizes of 10 or more to get trustworthy min/max/median/p90 startup stats. - Optimization History: A catalog of 12+ measured approaches covering what reduced startup time (route extraction, subpath imports, AOT zod-guard precompilation) and what did not (minification, blanket dynamic imports). - Local Profiling Guidance: Profile the built bundle with wrangler check startup across 10 serial runs, mapping minified frames through sourcemaps to attribute cost by package. - Use Case: When a deploy fails with error 10021, use this Skill to profile the bundle locally, identify the heaviest top-level evaluation cost, and verify the fix with a paired timing workflow run before merging. ## Quick Start Ask the agent to measure cfw-api startup time by running the upload timing script with a sample size of 10 and comparing the results against the recorded baseline.

Frequently Asked Questions about cfw-api-startup-optimization

High-intent search queries and answers about installing and using this skill.

FAQPage Schema
How do I measure Cloudflare Worker startup time accurately?▼

Use the upload timing workflow or the local timing script with a sample size of at least 10 to get min, max, median, and p90 stats. Startup time has high variance, so single runs are noise; compare strictly serial alternating A/B blocks rather than concurrent runs.

Why does my Cloudflare Worker deploy fail with error 10021?▼

Error 10021 means script startup exceeded the 1-second CPU time limit during Cloudflare's validation. Startup time covers V8 parsing plus all top-level code execution such as Zod schema construction, so reducing top-level evaluation work is the fix, not minification.

Does minification reduce Cloudflare Worker startup time?▼

No. Minification shrinks bundle bytes but startup CPU time stays identical because V8 performs the same top-level evaluation work. It was measured in PR #21204 with no startup improvement, though it remains useful for upload size.

How do I profile Cloudflare Worker startup locally?▼

Run wrangler check startup against the already-built bundle, aggregating 10 serial runs into cpuprofiles. Map minified frames through the build sourcemap and aggregate self time by npm package, since raw subtree names are misleading.

What optimizations actually reduced cfw-api startup time?▼

Route extraction to separate workers saved about 100ms, subpath imports replaced barrel imports for tree-shaking, and AOT precompilation of buildZodGuard with zod-compiler cut the median by about 41ms. Dynamic import of the router was measured and removed after showing no startup benefit.

When should I avoid dynamic import() in Cloudflare Workers?▼

Avoid it for route handlers that must register synchronously, since it breaks OpenAPI schema generation and causes miniflare test hangs. It works only for routes hidden from OpenAPI, such as the backfill route in cfw-api.