Skip to content

Pre-compress web assets using Brotli#57969

Merged
ryanclark merged 2 commits intomasterfrom
ryan/brotli
Aug 19, 2025
Merged

Pre-compress web assets using Brotli#57969
ryanclark merged 2 commits intomasterfrom
ryan/brotli

Conversation

@ryanclark
Copy link
Copy Markdown
Member

As it stands, we bundle the full JS/SVG/WASM files from the webassets build into the Teleport binary, and then gzip compress these on each request for the file. This is unnecessary as it both adds to the initial loading time of the app (before cache) and increases the teleport binary size.

This adds Brotli compression to the Vite build (can be disabled with an environment variable for local builds as it does slow the build a bit), and updates the proxy to stream the compressed file straight to the browser.

Brotli is ~20% smaller than gzipping and all major browsers support it

Stats

Metric Edition Before After Change % Change
teleport binary size OSS 324M 317M -7M -2.2%
e 367M 356M -11M -3.0%
Time to load app.js (no cache) e 96.6ms 28.4ms -68.2ms -70.6%
app.js size over the network e 1312kB (gzip on demand) 998kB (brotli) -314kB –23.9%

This is a backwards compatible change as the requests from the UI to the proxy for assets stay the same and old proxies will serve the normal JS, new proxies will serve the Brotli compressed version

changelog: Webassets are now pre-compressed with Brotli

@zmb3
Copy link
Copy Markdown
Collaborator

zmb3 commented Aug 16, 2025

Love it!

Copy link
Copy Markdown
Contributor

@avatus avatus left a comment

Choose a reason for hiding this comment

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

neat!

Comment thread lib/web/brotlihandler.go
return
}

io.Copy(w, brFile)
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

io.Copy can error but we aren't handling it here. are we ok with silent failing? second, is there any benefit to using something like http.ServeContent instead?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

I didn't think we needed all of the extra stuff http.ServeContent offers here as we handle the e-tag elsewhere and don't need range support (and http.ServeContent doesn't error as it doesn't return the error from the io.CopyN call), but I don't mind changing it

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

thats fine then. its not like we can set headers or anything after io.Copy has failed and if ServeContent doesnt return the io.Copy errors then we can skip it too. The client will just see a corrupted response (not like theyd be able to see anything else at this point tho so), ok LGTM

Copy link
Copy Markdown
Contributor

@gzdunek gzdunek left a comment

Choose a reason for hiding this comment

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

🚀

Comment thread lib/web/brotlihandler.go Outdated
ryanclark and others added 2 commits August 19, 2025 10:50
Co-authored-by: Grzegorz Zdunek <gzdunek@users.noreply.github.com>
@ryanclark ryanclark added this pull request to the merge queue Aug 19, 2025
Merged via the queue into master with commit a987c5a Aug 19, 2025
43 checks passed
@ryanclark ryanclark deleted the ryan/brotli branch August 19, 2025 10:19
@backport-bot-workflows
Copy link
Copy Markdown
Contributor

@ryanclark See the table below for backport results.

Branch Result
branch/v18 Failed

ryanclark added a commit that referenced this pull request Aug 19, 2025
* Pre-compress web assets using Brotli

* Update lib/web/brotlihandler.go

Co-authored-by: Grzegorz Zdunek <gzdunek@users.noreply.github.com>

---------

Co-authored-by: Grzegorz Zdunek <gzdunek@users.noreply.github.com>
ryanclark added a commit that referenced this pull request Aug 19, 2025
* Pre-compress web assets using Brotli

* Update lib/web/brotlihandler.go

Co-authored-by: Grzegorz Zdunek <gzdunek@users.noreply.github.com>

---------

Co-authored-by: Grzegorz Zdunek <gzdunek@users.noreply.github.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants