One Markdown page per domain per day.
Minimal Cloudflare Worker that serves a single AI-generated markdown essay per hostname per UTC day. First request triggers generation; the result is cached at the edge and in a Durable Object so you pay for one model call per day per domain. Output is plaintext markdown displayed inside a monospace HTML shell with automatic light/dark.
What it uses
- Cloudflare Worker + Durable Object
- Cloudflare AI Gateway (OpenAI-compatible) pointing to xAI Grok 4.1 fast reasoning
- Edge cache 24h + stale-while-revalidate 1h
- Optional streaming path
/streamto show generation live on cache misses
Quick start
wrangler login(or export a CF_API_TOKEN with Workers/DO/AI Gateway edit perms).- Create an AI Gateway in the dashboard, add the xAI provider, copy the compat base
https://gateway.ai.cloudflare.com/v1/<account_id>/<gateway_id>/compat. - From repo root:
wrangler secret put XAI_API_KEY- optional:
wrangler secret put GATEWAY_TOKENif your Gateway uses a separate token - set env
GATEWAY_BASEto your compat URL (or edit the default placeholder insrc/worker.js).
wrangler deploy- Map your domains in Cloudflare Routes/DNS to this worker. Host header drives per-domain text.
Behavior
- One generation per host per UTC day; DO stores
{text, generatedAt}with ~27h TTL. - Edge cache keyed per host/day; headers:
ETag=host:date,X-Generated-On= date. - Prompt: philosophical, host-aware, ~220–400 words, H1 + H2 sections, optional single bullet list, italic closing line.
- Fallback text is deterministic if the AI call fails.
- Footer shows generation date and a right-aligned link “a @steipete project” → https://steipete.me.
- Streaming: request
/streamto stream the AI response when there’s no cache; the completed page is cached afterward for normal/hits.
Testing
wrangler devthen curl twice:curl -H "Host: yourdomain.test" http://127.0.0.1:8787and confirmX-Generated-Onstays fixed.- Check AI Gateway analytics to verify only one upstream call per domain per day.
- Stream test:
curl -N http://127.0.0.1:8787/streamon a fresh day to observe live output.
Config references
src/worker.js: worker logic, DO class, Gateway call.wrangler.toml: DO bindingDOMAIN_DO, entrypoint, migration tag.docs/spec.md: deeper architecture and ops notes.- CI:
.github/workflows/ci.ymlrunswrangler deploy --dry-run(needs GitHub secrets:CLOUDFLARE_API_TOKEN,XAI_API_KEY, andGATEWAY_BASE; optionalGATEWAY_TOKEN).