Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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
andhtml.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?
htmlrewrite
functions.Previously:
"Waiting" takes most of the load time.
External resources only begin downloading after the document is fully fetched.
Now:
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.
External resources start fetching almost immediately after the page starts getting received.