A very simple Cloudflare Worker link shortener that lets you choose a custom name.
GET /— serves the HTML form (with a random 10-char name pre-filled)GET /<name>— looks<name>up in KV and308-redirects to the targetPOST /— form-encodedname+location, creates a short link
| Path | What |
|---|---|
src/index.js |
The whole Worker |
src/icon.svg |
The logo, for reuse elsewhere (not served) |
wrangler.jsonc |
Worker name, entrypoint, KV binding |
package.json |
Scripts + wrangler dev dependency |
npm install
npm run dev # http://localhost:8787wrangler dev runs the Worker locally against a local KV store kept in
.wrangler/state/ — it is completely separate from production data, starts empty,
and is gitignored. Create a link through the form at http://localhost:8787, or
seed one directly:
npx wrangler kv key put --local --binding=linkstorage test serble.net
npx wrangler kv key list --local --binding=linkstorage
curl -si localhost:8787/test # -> 308 to https://serble.netCreating links needs Turnstile. .dev.vars (gitignored) holds Cloudflare's public test
keys so local dev always passes:
TURNSTILE_SITEKEY=1x00000000000000000000AA
TURNSTILE_SECRET=1x0000000000000000000000000000000AA
To run locally against real production KV instead, use npx wrangler dev --remote.
Reads are harmless; be aware that any POST in remote mode writes real short links.
Deploys are automatic. The Worker is connected to this repo via Workers Builds in the Cloudflare dashboard - pushing to the default branch builds and deploys it. Watch a build under the Worker's Deployments tab.
Set the Turnstile keys before the first push. Creation fails closed, so until both are
in place POST / returns 503 and the form cannot make links. Redirects are unaffected.
Both live in the Cloudflare dashboard (Worker → Settings → Variables):
TURNSTILE_SITEKEY as a plaintext variable, TURNSTILE_SECRET as a secret.
wrangler.jsonc sets keep_vars so deploys leave them alone.
To deploy by hand (normally unnecessary): npm run deploy.
Live logs: npm run tail.