Skip to content

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.json or wrangler.jsonc
  • export 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 Env types from wrangler types.

Conventions, not rules. Real projects rename and move things, so confirm against the repository.

Where to start reading

  1. The Wrangler config, to list bindings and triggers
  2. The fetch handler
  3. Durable Object classes, if any

How a request moves through the code

  1. 1A request reaches the Worker at the edge and calls fetch(request, env, ctx).
  2. 2The handler routes the request itself or with a framework like Hono.
  3. 3It reads and writes through bindings such as env.DB for D1 or env.CACHE for KV.
  4. 4Stateful coordination goes to a Durable Object stub fetched from env.MY_DO.
  5. 5Background work uses ctx.waitUntil or 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 scheduled do?
  • What state lives in Durable Objects, and why?

What trips people up

  • Secrets are bindings set with wrangler secret, so they appear in code as env.NAME but 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.