-
Notifications
You must be signed in to change notification settings - Fork 4k
[No QA] Compress web bundles with Brotli #101730
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
Merged
roryabraham
merged 8 commits into
Expensify:main
from
software-mansion-labs:collectioneur/compress-with-brotli
Sep 29, 2026
+79
−1
Merged
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
06599ad
add brotli compression to the build
collectioneur 28fe2bc
fix spell
collectioneur 3cea6e5
add comment
collectioneur 46541f5
add explicit filename to Brotli compression
collectioneur cbee5d1
add our own compression plugin
collectioneur 0bab458
Merge branch 'collectioneur/compress-with-brotli' of https://github.c…
collectioneur 8cd37e9
exclude ad-hoc when generating .br
collectioneur f5846dc
fix comment explanation
collectioneur File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,55 @@ | ||
| import type {Compiler} from '@rspack/core'; | ||
|
|
||
| import {promisify} from 'util'; | ||
| import zlib from 'zlib'; | ||
|
|
||
| const PLUGIN_NAME = 'BrotliCompressionPlugin'; | ||
|
|
||
| const brotliCompress = promisify(zlib.brotliCompress); | ||
|
|
||
| type Options = { | ||
| /** Every emitted asset whose name matches gets a `.br` twin. */ | ||
| test: RegExp; | ||
| }; | ||
|
|
||
| /** | ||
| * Rspack plugin that writes a Brotli 11 twin (`foo.js` -> `foo.js.br`) beside every emitted asset matching `test`. | ||
| * Every match gets one, however small or incompressible: the CDN rewrite appends `.br` blindly, and a missing twin doesn't even 404 — the SPA fallback answers 200 | ||
| * with the app shell, so the browser either fails to decode that HTML as Brotli or refuses to run it as a script. | ||
| */ | ||
| class BrotliCompressionPlugin { | ||
| private readonly options: Options; | ||
|
|
||
| constructor(options: Options) { | ||
| this.options = options; | ||
| } | ||
|
|
||
| apply(compiler: Compiler): void { | ||
| const {Compilation, sources} = compiler.rspack; | ||
|
|
||
| // `thisCompilation` skips child compilations, whose assets end up in the parent's anyway. | ||
| compiler.hooks.thisCompilation.tap(PLUGIN_NAME, (compilation) => { | ||
| compilation.hooks.processAssets.tapPromise( | ||
| // OPTIMIZE_TRANSFER runs after minification, content hashing, HTML and the service worker are emitted, | ||
| // so the twins are made from the final bytes that will be deployed. | ||
| {name: PLUGIN_NAME, stage: Compilation.PROCESS_ASSETS_STAGE_OPTIMIZE_TRANSFER}, | ||
| async () => { | ||
| const assets = compilation.getAssets().filter(({name, info}) => !info.compressed && this.options.test.test(name)); | ||
|
|
||
| // zlib runs each compression on the libuv thread pool, so these proceed in parallel. | ||
| await Promise.all( | ||
| assets.map(async ({name, source, info}) => { | ||
| const compressed = await brotliCompress(source.buffer(), {params: {[zlib.constants.BROTLI_PARAM_QUALITY]: zlib.constants.BROTLI_MAX_QUALITY}}); | ||
|
|
||
| // `compressed` is the webpack convention that tells other plugins (and this one on a rebuild) not to | ||
| // compress the twin again. A twin of a content-hashed file is as immutable as its original. | ||
| compilation.emitAsset(`${name}.br`, new sources.RawSource(compressed), {compressed: true, immutable: info.immutable}); | ||
| }), | ||
| ); | ||
| }, | ||
| ); | ||
| }); | ||
| } | ||
| } | ||
|
|
||
| export default BrotliCompressionPlugin; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
NAB:
/docs/*can silently return the NewDot app shell.The
disthere combines the web build (which has.brtwins) with the Storybook build under./dist/docs(which does not).Because the CDN rewrite currently applies to
/docs/*, a request like:can miss the file and still return 200 HTML because of the SPA fallback. This can cause
ERR_CONTENT_DECODING_FAILEDor the browser to reject the script due to thetext/htmlMIME type. A 404-based check won't catch it.Could we please:
/docs/*from the CloudFront rewrite and add a comment explaining why.BrotliCompressionPlugin.ts:17— a missing twin does not necessarily mean a 404 because of the SPA fallback.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
cc: @justinpersaud
I think we need this since
/docsis the Storybook build, which doesn't go through rspack, so none of its assets have.brtwins, but they land in the same bucket behind the same distribution. WDYT?