Skip to content

Conversation

@mikenomitch
Copy link
Contributor

@mikenomitch mikenomitch commented Jan 30, 2025

Summary

Adds documentation and a changelog for recent updates to HTMLRewriter that allow for streamed content.

Screenshot 2025-01-30 at 3 34 42 PM


import { Render, TypeScriptExample } from "~/components";

[`HTMLRewriter`](/workers/runtime-apis/html-rewriter) now supports replacing HTML with content from a stream.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

speak 2nd person — you can now...


[`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.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

before and after?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it's just before because you would've await'd response.body previously, then passed in the string

Copy link
Contributor

@irvinebroque irvinebroque left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Think we can do a bit more here to speak directly to use case

ex:

  • I have some site, some origin
  • I have a Worker in front
  • In my Worker, I need to transform the HTML response — for example, I need to insert a <script> tag into the page
  • Before — I had to fetch the entire response object from the origin, and only after getting the whole response, I could pass that response into HTMLRewriter. This made things slower, because it prevented streaming the bytes of the response back to the client, as I receive them from the origin
  • After — I can pass the response directly in, and HTMLRewriter can immediately start parsing and handling rewriting content as it receives bytes of the response, and stream that back to the client immediately

@cloudflare-workers-and-pages
Copy link

cloudflare-workers-and-pages bot commented Jan 30, 2025

Deploying cloudflare-docs with  Cloudflare Pages  Cloudflare Pages

Latest commit: 3dcbcaf
Status: ✅  Deploy successful!
Preview URL: https://326df2ed.cloudflare-docs-7ou.pages.dev
Branch Preview URL: https://mnomitch-streaming-html-rewr.cloudflare-docs-7ou.pages.dev

View logs

@github-actions
Copy link
Contributor

@mikenomitch
Copy link
Contributor Author

@irvinebroque - gave it another pass

@mikenomitch mikenomitch enabled auto-merge (squash) January 30, 2025 23:35
@mikenomitch mikenomitch disabled auto-merge January 30, 2025 23:35
@mikenomitch mikenomitch merged commit 4c41b6f into production Jan 31, 2025
12 checks passed
@mikenomitch mikenomitch deleted the mnomitch/streaming-html-rewriter branch January 31, 2025 01:25
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

product:workers Related to Workers product size/s

Projects

None yet

Development

Successfully merging this pull request may close these issues.

9 participants