---
title: Transform HTML quickly with streaming content
description: HTMLRewriter now supports streamed content for more efficient replacement of HTML elements
image: https://edgetunnel-b2h.pages.dev/changelog-preview.png
---

> Documentation Index  
> Fetch the complete documentation index at: https://edgetunnel-b2h.pages.dev/changelog/llms.txt  
> Use this file to discover all available pages before exploring further. 

[Skip to content](#%5Ftop) 

# Changelog

New updates and improvements at Cloudflare.

[ Subscribe to RSS ](https://edgetunnel-b2h.pages.dev/changelog/rss/index.xml) [ View RSS feeds ](https://edgetunnel-b2h.pages.dev/fundamentals/new-features/available-rss-feeds/) 

![hero image](https://edgetunnel-b2h.pages.dev/_astro/hero.CVYJHPAd_26AMqX.svg) 

[ ← Back to all posts ](https://edgetunnel-b2h.pages.dev/changelog/) 

## Transform HTML quickly with streaming content

Jan 31, 2025 

[ Workers ](https://edgetunnel-b2h.pages.dev/workers/) 

You can now transform HTML elements with streamed content using [HTMLRewriter](https://edgetunnel-b2h.pages.dev/workers/runtime-apis/html-rewriter).

Methods like `replace`, `append`, and `prepend` now accept [Response](https://edgetunnel-b2h.pages.dev/workers/runtime-apis/response/) and [ReadableStream](https://edgetunnel-b2h.pages.dev/workers/runtime-apis/streams/readablestream/)values as [Content](https://edgetunnel-b2h.pages.dev/workers/runtime-apis/html-rewriter/#global-types).

This can be helpful in a variety of situations. For instance, you may have a Worker in front of an origin, and want to replace an element with content from a different source. Prior to this change, you would have to load all of the content from the upstream URL and convert it into a string before replacing the element. This slowed down overall response times.

Now, you can pass the `Response` object directly into the `replace` method, and HTMLRewriter will immediately start replacing the content as it is streamed in. This makes responses faster.

* [  JavaScript ](#tab-panel-2945)
* [  TypeScript ](#tab-panel-2946)

**index.js**

```js
class ElementRewriter {
  async element(element) {
    // 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) {
    let response = await fetch("https://site-to-replace.com");
    return new HTMLRewriter()
      .on("[data-to-replace]", new ElementRewriter())
      .transform(response);
  },
};
```

**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>;
```

For more information, see the [HTMLRewriter documentation](https://edgetunnel-b2h.pages.dev/workers/runtime-apis/html-rewriter).

```json
{"@context":"https://schema.org","@type":"BlogPosting","@id":"https://edgetunnel-b2h.pages.dev/changelog/post/2025-01-31-html-rewriter-streaming/#page","headline":"Transform HTML quickly with streaming content · Changelog","description":"HTMLRewriter now supports streamed content for more efficient replacement of HTML elements","url":"https://edgetunnel-b2h.pages.dev/changelog/post/2025-01-31-html-rewriter-streaming/","inLanguage":"en","image":"https://edgetunnel-b2h.pages.dev/changelog-preview.png","dateModified":"2025-01-31","datePublished":"2025-01-31","publisher":{"@type":"Organization","name":"Cloudflare","url":"https://www.cloudflare.com/"},"isPartOf":{"@type":"WebSite","@id":"https://edgetunnel-b2h.pages.dev/#website","name":"Cloudflare Docs","url":"https://edgetunnel-b2h.pages.dev/"}}
```
