---
source: https://iinventedlight.com/library/notes/cloudflare-htmlrewriter.md
title: "Cloudflare HTMLRewriter: parse and transform HTML in Workers"
license: CC-BY-4.0
license_url: https://creativecommons.org/licenses/by/4.0/
source_license: "CC-BY-4.0 (Cloudflare docs); CC-BY-4.0 (Cloudflare docs source); prose restates cited source lines in own words"
license_family: mixed-green
attribution: "Summary by iinventedlight (AI-assisted). Sources: Cloudflare docs, HTMLRewriter; Cloudflare docs source, html-rewriter.mdx. CC-BY-4.0 (https://raw.githubusercontent.com/cloudflare/cloudflare-docs/production/LICENSE). Copyright Cloudflare, Inc."
author: "iinventedlight (synthesis of cited sources); text drafted with AI assistance"
date: 2026-11-18
status: published
approved_by: "Cole Withington, 2026-11-20 13:14 PT: \"Publish the full batch and rewrite the 6 live notes\""
checked_on: 2026-11-18
as_of: 2026-11-18
confidence: medium
untested: "Not reproduced (2026-11-18). Official docs and source files were read; no test suite, server, or API call was run for this note."
category: coding-references
topic_area: "code-web: cloudflare htmlrewriter"
tags: [cloudflare, workers, htmlrewriter, html, streaming]
client: Librarian
sources:
  - id: S1
    title: "HTMLRewriter"
    publisher: Cloudflare docs
    url: https://developers.cloudflare.com/workers/runtime-apis/html-rewriter/
    accessed: 2026-11-18
    license: "CC-BY-4.0"
  - id: S2
    title: "html-rewriter.mdx"
    publisher: Cloudflare docs source
    url: https://raw.githubusercontent.com/cloudflare/cloudflare-docs/production/src/content/docs/workers/runtime-apis/html-rewriter.mdx
    accessed: 2026-11-18
    license: "CC-BY-4.0"
---
# Cloudflare HTMLRewriter: parse and transform HTML in Workers
**Answer:** HTMLRewriter is a streaming API to parse and transform HTML in Cloudflare Workers. You attach handlers with on for elements and onDocument for the document, then transform a response.

## Examples

```js
new HTMLRewriter()
  .on("*", new ElementHandler())
  .onDocument(new DocumentHandler());
```

## Notes

 It is part of the Workers runtime APIs.

## Reference list

- `HTMLRewriter` is a streaming API to parse and transform HTML.
- `Handlers` are attached with on and onDocument.
- `HTMLRewriter` parses and transforms HTML.

## See also

- [cloudflare-workers-fetch-handler](https://iinventedlight.com/library/notes/cloudflare-workers-fetch-handler/)
- [node-http-and-url](https://iinventedlight.com/library/notes/node-http-and-url/)
- [react-components-basics](https://iinventedlight.com/library/notes/react-components-basics/)

## Claims

- id: C1
  claim: HTMLRewriter is a streaming API to parse and transform HTML.
  source: S2
  evidence: "API to parse and transform HTML"

- id: C2
  claim: Handlers are attached with on and onDocument.
  source: S2
  evidence: "on(\"*\", new ElementHandler()) .onDocument(new DocumentHandler())"

- id: C3
  claim: HTMLRewriter parses and transforms HTML.
  source: S1
  evidence: "parse and transform HTML"

## Sources

- [S1] HTMLRewriter. Cloudflare docs. https://developers.cloudflare.com/workers/runtime-apis/html-rewriter/ . License: CC-BY-4.0. Accessed 2026-11-18.

- [S2] html-rewriter.mdx. Cloudflare docs source. https://raw.githubusercontent.com/cloudflare/cloudflare-docs/production/src/content/docs/workers/runtime-apis/html-rewriter.mdx . License: CC-BY-4.0. Accessed 2026-11-18.

License: this note is CC BY 4.0 (https://creativecommons.org/licenses/by/4.0/). Summary by iinventedlight (AI-assisted). Sources: Cloudflare docs, HTMLRewriter; Cloudflare docs source, html-rewriter.mdx. CC-BY-4.0 (https://raw.githubusercontent.com/cloudflare/cloudflare-docs/production/LICENSE). Copyright Cloudflare, Inc. Text was condensed and changed from the originals.

---

Agents: [/agents.md](/agents.md) | [/.well-known/agent-library.json](/.well-known/agent-library.json).
