Skip to content
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

Streaming injections #212

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open

Conversation

anfragment
Copy link
Owner

@anfragment anfragment commented Jan 21, 2025

What does this PR do?

Introduces streaming for HTML HTTP responses.
Currently, injecting CSS/JS into HTML responses requires us to read the entire response body first. This negates many browser performance optimizations, such as the ability to load external resources in parallel with the main document and do speculative parsing.
This change makes use of Go's io.Pipe and html.Tokenizer to enable non-blocking, on-the-fly modifications of HTML responses. HTML is also no longer matched via regular expressions, which evidently were a bottleneck. This significantly improves performance for end users by allowing browsers to better utilize their optimizations.


Additionally, this PR introduces a more accurate method for handling hop-to-hop headers during CONNECT proxying. For more details, see this comment.

How did you verify your code works?

  1. Existing automated tests for scriptlet injections.
  2. New automated tests for htmlrewrite functions.
  3. Manually inspected request waterfalls in different browsers.

Previously:

image

"Waiting" takes most of the load time.

image

External resources only begin downloading after the document is fully fetched.

Now:

image

TTFB is improved 3x (the initial delay is almost entirely due to round-trip time). Full page loads are 1.5-2x faster on average.

image

External resources start fetching almost immediately after the page starts getting received.

…vely close connections when clients' or servers' transmit the intention to do so themselves
@anfragment anfragment added the review Completed and ready for evaluation label Jan 21, 2025
@anfragment anfragment self-assigned this Jan 21, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
review Completed and ready for evaluation
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants