valfz

course

Workers from first principles

Build a link shortener on Cloudflare Workers with the cf CLI: routing, bindings, KV, D1, static assets, tests and deployment.

topic
cloudflare workers
level
introductory
lessons
8
read
1 h 30 min

This course is for developers who write JavaScript or TypeScript and know HTTP, and who have not built on Cloudflare Workers. It starts from what the runtime is (an isolate, with rules a server process does not have) and adds one platform feature per lesson, each time because the project needs it.

The project is a link shortener. It accepts a URL and returns a short code, redirects visitors who follow the code, records each visit, and serves a small HTML front end. Links live in KV, visits in D1, and the front end ships as static assets of the same Worker.

The course uses Cloudflare’s cf CLI and the typed cloudflare.config.ts file. It does not use Wrangler or wrangler.jsonc. Every command and code sample was run with these versions:

Tool Version
cf 1.0.0-rc.6
@cloudflare/vite-plugin 2.0.0-rc.6
@cloudflare/vitest-plugin 1.10.0
workerd 1.20261127.1
Vite+ / Vitest 1.4.0 / 5.0.5
TypeScript 7.0.2
Node.js / pnpm 24.22.2 / 12.16.1

cf is a release candidate, so pin the version as the lessons do. Lessons 1 to 7 run entirely on your machine and need no Cloudflare account. Lesson 8 deploys; its remote commands are shown with dry-run output.

Type the code instead of pasting it, run every command, and compare your output with the output shown. Each lesson ends with the project in a working state and the next one starts from there.

What you build

A link shortener that creates short codes, redirects visitors, counts visits and serves a small static front end.

Before you start

  • JavaScript or TypeScript, including async functions and modules
  • HTTP basics: methods, status codes and headers
  • A terminal with Node.js 24 and pnpm

After the last lesson you can

  • Explain how an isolate differs from a server process and what that means for state
  • Write a fetch handler with routing, validation and error handling using platform APIs only
  • Declare bindings, secrets and modes in cloudflare.config.ts
  • Store data in KV and D1 and say why each piece of data lives where it does
  • Serve static files from the same Worker and control which requests reach your code
  • Test a Worker inside the Workers runtime and deploy it with cf

lessons8, in order

  1. What a Worker is8 minHow Cloudflare runs your code in V8 isolates, what that means for state and startup, and which limits to design around.
  2. A first Worker8 minCreate the course project with cf init, read every generated file, write a fetch handler and run it locally with cf dev.
  3. Requests, routing and responses14 minBuild the shortener's three routes with URLPattern, Request and Response, and put validation and error handling in one place.
  4. Configuration and bindings11 minDeclare text, JSON and secret bindings in cloudflare.config.ts, protect link creation with a token, and vary values by mode.
  5. Key-value storage with KV11 minMove links from memory into Workers KV, add expiring links, and learn what eventual consistency means for a shortener.
  6. Relational data with D112 minAdd a D1 database with a migration, record visits with prepared statements, and use ctx.waitUntil so the write never delays the redirect.
  7. Static assets13 minServe the front end from the same Worker and control which requests are answered from files and which reach your code.
  8. Testing and shipping13 minWrite tests that run inside the Workers runtime, inspect the build, then deploy with real resources, secrets, a domain and logs.

keyboard

h
home
w c a
writing, courses, about
n p
next and previous lesson
j k
scroll down and up
g G
top and bottom of the page
t
switch light and dark
?
this help

shortcuts are