Skip to main content

Overview

HTMLRewriter lets you modify HTML responses as they stream through your edge script. It parses HTML on the fly and calls your handler functions when it encounters matching elements, comments, or text without buffering the entire document in memory.

Quick Start

A simple middleware example:

Constructor

object

Methods

on(selector, handlers)

Registers handlers for elements matching a CSS selector. Returns this for chaining.
string
required
A CSS selector. See supported selectors below.
ElementHandlers
required
An object with optional handler functions. Each handler can be sync or async.

onDocument(handlers)

Registers document-level handlers. Returns this for chaining.
DocumentHandlers
required

transform(response)

Applies all registered handlers to the response body and returns a new Response.
Response
required
The HTTP response to transform. Must not be an error response.
Returns: A new Response with:
  • The same headers (minus Content-Length, since the body length may change)
  • A streaming body with the transformed HTML

Handler Types

Element

Passed to element handlers. Represents an HTML opening tag.

Properties

Attribute Methods

getAttribute(name)
Returns the value of the attribute with the given name, or null if the attribute does not exist.
string
required
The attribute name.
Returns: string | null
hasAttribute(name)
Returns whether the element has an attribute with the given name.
string
required
The attribute name.
Returns: boolean
setAttribute(name, value)
Sets the value of the attribute with the given name. Adds the attribute if it does not exist.
string
required
The attribute name.
string
required
The attribute value.
Returns: Element — the element itself, for chaining.
removeAttribute(name)
Removes the attribute with the given name. No-op if the attribute does not exist.
string
required
The attribute name.
Returns: Element — the element itself, for chaining. Setters return the element itself, so calls can be chained:

Content Mutation Methods

All content mutation methods accept content as a string, ReadableStream<Uint8Array>, or Response, and an optional options object. They all return Element for chaining.
string | ReadableStream<Uint8Array> | Response
required
The content to insert. Strings are inserted directly. Streams and Response bodies are consumed and piped into the output.
boolean
default:false
When true, content is inserted as raw HTML. When false, content is escaped as text.
before(content, options?)
Inserts content immediately before the element’s opening tag. Returns: Element
after(content, options?)
Inserts content immediately after the element’s closing tag. Returns: Element
prepend(content, options?)
Inserts content at the beginning of the element, right after the opening tag. Returns: Element
append(content, options?)
Inserts content at the end of the element, right before the closing tag. Returns: Element
replace(content, options?)
Replaces the entire element (opening tag, content, and closing tag) with the provided content. Returns: Element
setInnerContent(content, options?)
Replaces the element’s inner content, keeping the opening and closing tags. Returns: Element

Removal Methods

remove()
Removes the element and all of its content (opening tag, children, closing tag). Returns: Element
removeAndKeepContent()
Removes the element’s opening and closing tags but keeps the inner content in place. Returns: Element

End Tag Handler

onEndTag(handler)
Registers a handler that is called when the element’s closing tag is encountered.
(endTag: EndTag) => void | Promise<void>
required
A callback receiving the EndTag object. Can be async.
Returns: void

Comment

Passed to comments handlers.

Properties

Methods

before(content, options?)
Inserts content immediately before the comment.
string | ReadableStream<Uint8Array> | Response
required
The content to insert.
boolean
default:false
When true, content is inserted as raw HTML. When false, content is escaped as text.
Returns: Comment
after(content, options?)
Inserts content immediately after the comment.
string | ReadableStream<Uint8Array> | Response
required
The content to insert.
boolean
default:false
When true, content is inserted as raw HTML. When false, content is escaped as text.
Returns: Comment
replace(content, options?)
Replaces the comment with the provided content.
string | ReadableStream<Uint8Array> | Response
required
The content to replace with.
boolean
default:false
When true, content is inserted as raw HTML. When false, content is escaped as text.
Returns: Comment
remove()
Removes the comment from the document. Returns: Comment

TextChunk

Passed to text handlers. Note: a single text node may be split across multiple chunks.

Properties

Methods

before(content, options?)
Inserts content immediately before the text chunk.
string | ReadableStream<Uint8Array> | Response
required
The content to insert.
boolean
default:false
When true, content is inserted as raw HTML. When false, content is escaped as text.
Returns: TextChunk
after(content, options?)
Inserts content immediately after the text chunk.
string | ReadableStream<Uint8Array> | Response
required
The content to insert.
boolean
default:false
When true, content is inserted as raw HTML. When false, content is escaped as text.
Returns: TextChunk
replace(content, options?)
Replaces the text chunk with the provided content.
string | ReadableStream<Uint8Array> | Response
required
The content to replace with.
boolean
default:false
When true, content is inserted as raw HTML. When false, content is escaped as text.
Returns: TextChunk
remove()
Removes the text chunk from the document. Returns: TextChunk

EndTag

Passed to onEndTag handlers.

Properties

Methods

before(content, options?)
Inserts content immediately before the end tag.
string | ReadableStream<Uint8Array> | Response
required
The content to insert.
boolean
default:false
When true, content is inserted as raw HTML. When false, content is escaped as text.
Returns: EndTag
after(content, options?)
Inserts content immediately after the end tag.
string | ReadableStream<Uint8Array> | Response
required
The content to insert.
boolean
default:false
When true, content is inserted as raw HTML. When false, content is escaped as text.
Returns: EndTag
remove()
Removes the end tag from the document. Returns: EndTag

Doctype

Passed to doctype handlers. All properties are read-only.

Properties

DocumentEnd

Passed to end handlers.

Methods

append(content, options?)
Appends content at the end of the document.
string
required
The content to append. Only accepts string (not streams or responses).
boolean
default:false
When true, content is inserted as raw HTML. When false, content is escaped as text.
Returns: DocumentEnd

Supported CSS Selectors

The following CSS selectors are supported, based on the W3C Selectors Level 4 specification.

Examples

Inject a Script

Remove Elements

Async Handler

Handlers can return a Promise for async operations like sub-requests.

Class-Based Handlers

Instead of inline objects, you can define handler classes and pass instances to .on() or .onDocument().

Document Handler Class

Async Class Handler

Class methods can be async just like inline handlers.

Multiple Selectors

Chain multiple .on() calls to handle different elements independently.

References