How to understand a Cloudflare Workers codebase
A Worker exports an object with handlers such as `fetch`, `scheduled` and `queue`. Everything it can reach outside its own code (databases, storage, other Workers, secrets) arrives as bindings on the `env` argument, configured in Wrangler, so the config file is half of the architecture.
How to tell it's Cloudflare Workers
wrangler.toml,wrangler.jsonorwrangler.jsoncexport default { async fetch(request, env, ctx) { ... } }- Exported Durable Object classes and a generated
worker-configuration.d.ts
Where things usually live
- wrangler.toml / wrangler.jsonc
- Entry file, compatibility date, bindings (KV, D1, R2, Durable Objects, Queues), routes, crons.
- src/index.ts
- The exported handlers.
- src/durable-objects/
- Durable Object classes, each with its own state and storage.
- migrations/
- D1 SQL migrations.
- worker-configuration.d.ts
- Generated
Envtypes fromwrangler types.
Conventions, not rules. Real projects rename and move things, so confirm against the repository.
Where to start reading
- The Wrangler config, to list bindings and triggers
- The
fetchhandler - Durable Object classes, if any
How a request moves through the code
- A request reaches the Worker at the edge and calls
fetch(request, env, ctx). - The handler routes the request itself or with a framework like Hono.
- It reads and writes through bindings such as
env.DBfor D1 orenv.CACHEfor KV. - Stateful coordination goes to a Durable Object stub fetched from
env.MY_DO. - Background work uses
ctx.waitUntilor sends a message to a Queue.
Questions to ask about the repository
- List every binding and where each one is used.
- Which cron triggers exist, and what does
scheduleddo? - What state lives in Durable Objects, and why?
What trips people up
- Secrets are bindings set with
wrangler secret, so they appear in code asenv.NAMEbut not in the config file. - There is no long-running process; global variables may or may not persist between requests.
- The compatibility date changes runtime behaviour; check it when behaviour differs from docs.