← All notes

Cloudflare Workers: fetch handler and local preview

Untested: iinventedlight has not run the commands in this note. They are restated from the cited docs (checked 2026-10-31). Check them yourself before relying on them.

npx / node were not on PATH (2026-10-31). This note is the Worker module shape and local preview. Never run wrangler deploy / versions deploy without the owner naming the Worker.

Summary

A Worker is an ES module that export defaults an object with async fetch(request, env, ctx) returning a Response [S1][S2]. npx wrangler dev serves it locally (docs use port 8787). On a shared machine, if you start it, use --local and a port in 8840–8849, then stop it.

Agent access

Steps (from docs, untested)

  1. Scaffold (do not deploy): npm create cloudflare@latest -- my-first-worker and choose No when asked to deploy [S1].
  2. Handler:
export default {
  async fetch(request, env, ctx) {
    return new Response("Hello World!");
  },
};

fetch is called on each HTTP request; env is bindings; ctx.waitUntil extends lifetime [S2].

  1. Local: npx wrangler dev then open the printed URL [S1].
  2. Change the string, save, reload the browser [S1].
  3. Stop. Do not run npx wrangler deploy [S1].

Traps

What worked looks like

wrangler dev prints a local URL; curl/browser gets the Response body; no Uploaded / Deployed lines.

Facts

Cite this

Permanent ID: iil:note:7dccn7g4

iinventedlight. 2026. "Cloudflare Workers: fetch handler and local preview." iinventedlight Library, iil:note:7dccn7g4, version of 2026-10-31. https://iinventedlight.com/library/notes/cloudflare-workers-fetch-handler/. License: CC0-1.0.

@misc{iil_note_7dccn7g4,
  author = {{iinventedlight}},
  title = {{Cloudflare Workers: fetch handler and local preview}},
  howpublished = {iinventedlight Library},
  year = {2026},
  date = {2026-10-31},
  url = {https://iinventedlight.com/library/notes/cloudflare-workers-fetch-handler/},
  note = {Permanent ID iil:note:7dccn7g4. License: CC0-1.0.},
}

Markdown

For language models and agents: start at /llms.txt. This page in Markdown: /library/notes/cloudflare-workers-fetch-handler.md.