-
Notifications
You must be signed in to change notification settings - Fork 10.3k
Adds streaming HTMLRewriter content docs and changelog #19578
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,40 @@ | ||
| --- | ||
| title: Rewrite HTML with streaming content | ||
| description: HTMLRewiter now supports streamed content for more efficient replacement of HTML elements | ||
| products: | ||
| - workers | ||
| date: 2025-02-03T3:00:00Z | ||
| --- | ||
|
|
||
| import { Render, TypeScriptExample } from "~/components"; | ||
|
|
||
| [`HTMLRewriter`](/workers/runtime-apis/html-rewriter) now supports replacing HTML with content from a stream. | ||
|
||
|
|
||
| This allows you to process HTML more efficiently, as content does not have to be loaded into memory before replacements are made. | ||
|
||
|
|
||
| [`Response`](/workers/runtime-apis/response/) and [`ReadableStream`](/workers/runtime-apis/streams/readablestream/) values can now | ||
| be passed as [`Content`](/workers/runtime-apis/html-rewriter/#global-types) into methods duch as | ||
| `replace`, `append`, and `prepend`. | ||
|
|
||
| <TypeScriptExample filename="index.ts"> | ||
| ```ts | ||
| class ElementRewriter { | ||
| async element(element: any) { | ||
| // able to replace elements while streaming content | ||
| // the fetched body is not buffered into memory as part | ||
| // of the replace | ||
| let res = await fetch('https://upstream-content-provider.example'); | ||
| element.replace(res); | ||
| } | ||
| } | ||
|
|
||
| export default { | ||
| async fetch(request, env, ctx): Promise<Response> { | ||
| let response = await fetch('https://site-to-replace.com'); | ||
| return new HTMLRewriter().on('[data-to-replace]', new ElementRewriter()).transform(response); | ||
| }, | ||
| } satisfies ExportedHandler<Env>; | ||
| ``` | ||
| </TypeScriptExample> | ||
|
|
||
| For more information, see the [`HTMLRewriter` documentation](/workers/runtime-apis/html-rewriter). | ||
Uh oh!
There was an error while loading. Please reload this page.