Workers - Transform HTML quickly with streaming content
1y ago
Source
CloudflareWorkers - Transform HTML quickly with streaming contentcloudflare.comYou can now transform HTML elements with streamed content using HTMLRewriter . Methods like replace , append , and prepend now accept Response and ReadableStream values as Content . 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 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 ( " ) ; element . replace ( res ) ; } } export default { async fetch ( request , env , ctx ) { let response = await fetch ( " ) ; return new HTMLRewriter () . on ( "[data-to-replace]" , new ElementRewriter ()) . transform ( response ) ; }, }; TypeScript 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 ( ' ) ; element . replace ( res ) ; } } export default { async fetch ( request , env , ctx ) : Promise < Response > { let response = await fetch ( ' ) ; return new HTMLRewriter () . on ( '[data-to-replace]' , new ElementRewriter ()) . transform ( response ) ; }, } satisfies ExportedHandler < Env >; For more information, see the HTMLRewriter documentation .
You might also wanna read
Chrome 148 introduces declarative partial updates for out-of-order HTML streaming
Chrome 148 introduces declarative partial updates, enabling out-of-order streaming of HTML content. This allows web developers to stream HTM
Chrome 148 introduces declarative partial updates for out-of-order HTML streaming
Chrome 148 introduces declarative partial updates, enabling out-of-order streaming of HTML content. This allows web developers to stream HTM
Porting JustHTML HTML5 Parser from Python to JavaScript Using Codex CLI and GPT-5.2
The author describes successfully porting JustHTML, a standards-compliant HTML5 parser originally written in Python, to JavaScript using Cod
simonwillison.net·6mo ago
Reimagining the Web Streams API: A Proposal for Modern JavaScript Streaming
The article critiques the current Web Streams API (WHATWG Streams Standard) as being designed for a different era with outdated constraints,
Bun v1.1.23
bun.com·1y ago
htmx 4 Released: Library Rebuilt with Modern fetch() API for Better Performance
The article announces htmx 4, a major update to the htmx JavaScript library that replaces its XMLHttpRequest-based implementation with the m
Show HN: String Flux – Simplify everyday string transformations for developers
stringflux.io·1y ago

Comments
Sign in to join the conversation.
No comments yet. Be the first.