user@gryczka.dev: ~/garden/cloudflare-edge-rewrite
$ cat ~/garden/cloudflare-edge-rewrite/README.md
Edge Rewrite — Streaming HTML Transformations at the Edge screenshot

Edge Rewrite — Streaming HTML Transformations at the Edge

July 23, 2026 | Patrick Gryczka

## Overview

A framework-free reference application for transforming existing HTML without buffering or rebuilding the origin. The Worker validates a deployment-controlled Wikipedia route, fetches and caches the source article, then composes streaming HTMLRewriter handlers for a dark editorial redesign, coarse request-location context, Spanish title and summary translation, and an AI-generated dad joke. A typed mode registry keeps routing, AI selection, stylesheets, link rewriting, and source-code links synchronized. Visitor credentials never reach the origin, scripts are removed, redirects remain origin-bound, AI output is sanitized before insertion, and a native Rate Limiting binding degrades paid inference to the plain article when the public-demo budget is unavailable.

## Highlights

  • -> Streaming HTMLRewriter handlers transform large origin pages without buffering the response body
  • -> One typed mode registry drives routing, AI capabilities, stylesheets, link rewriting, and source-code links
  • -> Workers AI translates bounded canonical summaries and generates sanitized article-specific dad jokes through AI Gateway caching
  • -> Read-only route policy, origin-bound redirects, credential isolation, script removal, restrictive CSP, and visible Wikimedia attribution
  • -> Per-mode Rate Limiting binding protects public AI usage and serves the plain article when the guardrail or model is unavailable

## Architecture

Edge Rewrite architecture diagram
Browser
    │   GET /<mode>/wiki/<article>
    ▼
Cloudflare Worker
    ├── read-only route + redirect policy
    ├── configured Wikipedia origin ── edge-cached HTML stream
    ├── request.cf ────────────────── coarse context only
    ├── Rate Limiting binding ─────── per-mode AI guardrail
    └── Workers AI + AI Gateway ───── translation + dad joke
            ▼
Streaming HTMLRewriter
    ├── compose mode handlers + rewrite links/assets
    ├── remove scripts + isolate credentials + apply CSP
    └── local CSS/font assets + source attribution
            ▼
Transformed article (private, no-store, noindex)

## Tech Stack

WorkersWorkers AIHTMLRewriterRate LimitingTypeScript
$ _