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
- CLI: yes (
npx wrangler,npm create cloudflare). MCP connector: Cloudflare MCP is connected on the iinventedlight project (docs/search/execute). API: Cloudflare REST. GUI: dashboard. - Best route: CLI for code/dev; MCP for reading account state.
- Permissions: local preview needs none. Deploy needs a Cloudflare login or API token with Workers edit, plus the owner's approval.
Steps (from docs, untested)
- Scaffold (do not deploy):
npm create cloudflare@latest -- my-first-workerand choose No when asked to deploy [S1]. - 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].
- Local:
npx wrangler devthen open the printed URL [S1]. - Change the string, save, reload the browser [S1].
- Stop. Do not run
npx wrangler deploy[S1].
Traps
- First
wrangler devmay open a browser forwrangler login[S1]. Prefer--localso nothing talks to the account. - XMLHttpRequest is not in the Workers runtime; use
fetch[S2]. fetchmust return aResponseor a Promise of one [S1][S2].npm create cloudflarecan deploy at the end of the wizard; answer No [S1].
What worked looks like
wrangler dev prints a local URL; curl/browser gets the Response body; no Uploaded / Deployed lines.
Facts
- id: F1 claim: Incoming HTTP requests are passed to export default's fetch(request, env, ctx), which must return a Response. confidence: high date: 2026-10-31 sources: [S1, S2] license: CC0 1.0 checked_on: 2026-10-31
- id: F2 claim: wrangler dev starts a local server for the Worker; wrangler deploy publishes it. confidence: high date: 2026-10-31 sources: [S1] license: CC0 1.0 checked_on: 2026-10-31
Cite this
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.},
}
For language models and agents: start at /llms.txt. This page in Markdown: /library/notes/cloudflare-workers-fetch-handler.md.