Skip to content

Commit 57cccf4

Browse files
committed
Adds streaming HTMLRewriter content docs and changelog
1 parent 1672a33 commit 57cccf4

File tree

2 files changed

+42
-2
lines changed

2 files changed

+42
-2
lines changed
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
---
2+
title: Rewrite HTML with streaming content
3+
description: HTMLRewiter now supports streamed content for more efficient replacement of HTML elements
4+
products:
5+
- workers
6+
date: 2025-02-03T3:00:00Z
7+
---
8+
9+
import { Render, TypeScriptExample } from "~/components";
10+
11+
[`HTMLRewriter`](/workers/runtime-apis/html-rewriter) now supports replacing HTML with content from a stream.
12+
13+
This allows you to process HTML more efficiently, as content does not have to be loaded into memory before replacements are made.
14+
15+
[`Response`](/workers/runtime-apis/response/) and [`ReadableStream`](/workers/runtime-apis/streams/readablestream/) values can now
16+
be passed as [`Content`](/workers/runtime-apis/html-rewriter/#global-types) into methods duch as
17+
`replace`, `append`, and `prepend`.
18+
19+
<TypeScriptExample filename="index.ts">
20+
```ts
21+
class ElementRewriter {
22+
async element(element: any) {
23+
// able to replace elements while streaming content
24+
// the fetched body is not buffered into memory as part
25+
// of the replace
26+
let res = await fetch('https://upstream-content-provider.example');
27+
element.replace(res);
28+
}
29+
}
30+
31+
export default {
32+
async fetch(request, env, ctx): Promise<Response> {
33+
let response = await fetch('https://site-to-replace.com');
34+
return new HTMLRewriter().on('[data-to-replace]', new ElementRewriter()).transform(response);
35+
},
36+
} satisfies ExportedHandler<Env>;
37+
```
38+
</TypeScriptExample>
39+
40+
For more information, see the [`HTMLRewriter` documentation](/workers/runtime-apis/html-rewriter).

src/content/docs/workers/runtime-apis/html-rewriter.mdx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,9 +30,9 @@ Throughout the `HTMLRewriter` API, there are a few consistent types that many pr
3030

3131

3232

33-
* `Content` string
33+
* `Content` string, Response, or ReadableStream
3434

35-
* Content inserted in the output stream should be a string.
35+
* Content inserted in the output stream should be a string, a [`Response`](/workers/runtime-apis/response/), or a [`ReadableStream`](/workers/runtime-apis/streams/readablestream/).
3636

3737
* `ContentOptions` Object
3838

0 commit comments

Comments
 (0)