Skip to content

[No QA] Compress web bundles with Brotli - #101730

Merged
roryabraham merged 8 commits into
Expensify:mainfrom
software-mansion-labs:collectioneur/compress-with-brotli
Sep 29, 2026
Merged

roryabraham merged 8 commits into
Expensify:mainfrom
software-mansion-labs:collectioneur/compress-with-brotli

Conversation

@collectioneur

@collectioneur collectioneur commented Sep 21, 2026 •

Copy link
Copy Markdown
Contributor

Explanation of Change

Emits a Brotli 11 .br twin of every js/css/html/svg/wasm/ttf asset at build time (via compression-webpack-plugin in config/rsbuild/rsbuild.common.ts) and uploads the twins to S3 with Content-Encoding: br and the original's Content-Type (deploy.yml), so the CDN can serve them to browsers that accept Brotli instead of gzipping on the fly.

Steps 1 and 2 of the linked issue. Steps 3 and 4 (CloudFront rewrite to uri + '.br' for Accept-Encoding: br, Brotli enabled on the Cloudflare zone) are infra changes outside this repo. Until they land, the .br objects sit unused and nothing user-facing changes.

Dependency note: compression-webpack-plugin is README-deprecated since 2026-09-19

  • On 2026-09-19 the maintainers deprecated compression-webpack-plugin in its README only (the npm package is not flagged, 12.0.0 works as before) and moved compression entirely into minimizer-webpack-plugin ≥ 5.11.0 as an asset generator (MinimizerPlugin.compress). That package is the renamed terser-webpack-plugin: 5.6.1 was its last release under the old name.
  • Rspack's compatibility table lists terser-webpack-plugin (the old name) as compatible and its test suite pins ^5.6.1. A change in the renamed package on 2026-08-27 (#701, option validation through webpack's validate hook, feature-detected with if (compiler.hooks.validate)) breaks it under Rspack, whose compiler.hooks proxy throws on unknown hook names instead of returning undefined. Verified locally with 5.11.0 on this build: it fails in apply with Compiler.hooks.validate is not supported in rspack. Using the successor today would need a patch-package patch, and 5.11.0 is additionally blocked by our .npmrc min-release-age=7 until 2026-09-26.
  • Rspack itself lists compression-webpack-plugin as compatible, so this PR ships with it. Next step: open an issue in Rspack to restore compatibility with the renamed plugin (expose compiler.hooks.validate), then migrate to minimizer-webpack-plugin in a follow-up.

Notes

  • threshold: 0, minRatio: Infinity - a twin for every matching file.
  • .br twins are excluded from the Workbox precache (the service worker requests the original URLs and the CDN serves the Brotli copy transparently) and from Rsbuild's file-size report.
  • deploy.yml uploads in two passes: everything except *.br exactly as before, then the twins per extension with --content-encoding br and an explicit --content-type (aws-cli would otherwise tag them binary/octet-stream). The MIME types match what the CDN serves for the originals today (.js is text/javascript).
  • Ad-hoc deploys skip *.br: that distribution has no Brotli rewrite.
  • Development builds skip the plugin.

Fixed Issues

$ #101250
PROPOSAL:

Tests

  1. Run npm run build-staging.
  2. In the repo root, list every deployable file that has no Brotli twin:
    find dist -type f \( -name '*.js' -o -name '*.css' -o -name '*.html' -o -name '*.svg' -o -name '*.wasm' -o -name '*.ttf' \) | while read -r f; do [ -f "$f.br" ] || echo "missing twin: $f"; done
  3. Verify that the command prints nothing: every js/css/html/svg/wasm/ttf file under dist/, including nested ones such as css/AnnotationLayer.css, has a .br copy next to it.

When deployed to staging:

  1. Open https://staging.new.expensify.com, sign in and use the app as usual. Verify that it loads and behaves exactly as before (nothing user-facing changes until the CDN rewrite is enabled).
  2. Reload the app while offline. Verify that it still opens from the service worker cache.
  • Verify that no errors appear in the JS console

Offline tests

N/A

QA Steps

No QA

  • Verify that no errors appear in the JS console

PR Author Checklist

  • I linked the correct issue in the ### Fixed Issues section above
  • I wrote clear testing steps that cover the changes made in this PR
    • I added steps for local testing in the Tests section
    • I added steps for the expected offline behavior in the Offline steps section
    • I added steps for Staging and/or Production testing in the QA steps section
    • I added steps to cover failure scenarios (i.e. verify an input displays the correct error message if the entered data is not correct)
    • I turned off my network connection and tested it while offline to ensure it matches the expected behavior (i.e. verify the default avatar icon is displayed if app is offline)
    • I tested this PR with a High Traffic account against the staging or production API to ensure there are no regressions (e.g. long loading states that impact usability).
  • I included screenshots or videos for tests on all platforms
  • I ran the tests on all platforms & verified they passed on:
    • Android: Native
    • Android: mWeb Chrome
    • iOS: Native
    • iOS: mWeb Safari
    • MacOS: Chrome / Safari
  • I verified there are no console errors (if there's a console error not related to the PR, report it or open an issue for it to be fixed)
  • I followed proper code patterns (see Reviewing the code)
    • I verified that comments were added to code that is not self explanatory
    • I verified that any new or modified comments were clear, correct English, and explained "why" the code was doing something instead of only explaining "what" the code was doing.
    • I verified any copy / text that was added to the app is grammatically correct in English. It adheres to proper capitalization guidelines (note: only the first word of header/labels should be capitalized), and is either coming verbatim from figma or has been approved by marketing (in order to get marketing approval, ask the Bug Zero team member to add the Waiting for copy label to the issue)
  • If a new code pattern is added I verified it was agreed to be used by multiple Expensify engineers
  • I followed the guidelines as stated in the Review Guidelines
  • I tested other components that can be impacted by my changes (i.e. if the PR modifies a shared library or component like Avatar, I verified the components using Avatar are working as expected)
  • If a new CSS style is added I verified that:
    • A similar style doesn't already exist
    • The style can't be created with an existing StyleUtils function (i.e. StyleUtils.getBackgroundAndBorderStyle(theme.componentBG))
  • If new assets were added or existing ones were modified, I verified that:
    • The assets are optimized and compressed (for SVG files, run npm run compress-svg)
    • The assets load correctly across all supported platforms.
  • If the PR modifies code that runs when editing or sending messages, I tested and verified there is no unexpected behavior for all supported markdown - URLs, single line code, code blocks, quotes, headings, bold, strikethrough, and italic.
  • If the PR modifies a generic component, I tested and verified that those changes do not break usages of that component in the rest of the App (i.e. if a shared library or component like Avatar is modified, I verified that Avatar is working as expected in all cases)
  • If the PR modifies a component related to any of the existing Storybook stories, I tested and verified all stories for that component are still working as expected.
  • If the PR modifies a component or page that can be accessed by a direct deeplink, I verified that the code functions as expected when the deeplink is used - from a logged in and logged out account.
  • If the PR modifies the UI (e.g. new buttons, new UI components, changing the padding/spacing/sizing, moving components, etc) or modifies the form input styles:
    • I verified that all the inputs inside a form are aligned with each other.
    • I added Design label and/or tagged @Expensify/design so the design team can review the changes.
  • I added unit tests for any new feature or bug fix in this PR to help automatically prevent regressions in this user flow.
  • If the main branch was merged into this PR after a review, I tested again and verified the outcome was still expected according to the Test steps.

@github-actions

Copy link
Copy Markdown
Contributor

⚠️ This PR is possibly changing native code and/or updating libraries, it may cause problems with HybridApp. Please check if any patch updates are required in the HybridApp repo and run an AdHoc build to verify that HybridApp will not break. Ask Contributor Plus for help if you are not sure how to handle this. ⚠️

@collectioneur collectioneur changed the title Add brotli compression to the build [No QA] Compress web bundles with Brotli Sep 21, 2026
@collectioneur

Copy link
Copy Markdown
Contributor Author

@codex review

Comment thread config/rsbuild/rsbuild.common.ts Outdated

@dariusz-biela dariusz-biela left a comment

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.

Looks good to me!

Note

One reminder for the follow-up CDN rewrite: Storybook assets under /docs/ don’t get .br copies, so exclude that path from the rewrite unless we also add compression for the Storybook build. This doesn’t block this PR.

@collectioneur
collectioneur marked this pull request as ready for review September 21, 2026 14:27
@collectioneur
collectioneur requested a review from a team as a code owner September 21, 2026 14:27
@melvin-bot
melvin-bot Bot requested review from marufsharifi and removed request for a team September 21, 2026 14:27
@melvin-bot

melvin-bot Bot commented Sep 21, 2026

Copy link
Copy Markdown

@marufsharifi Please copy/paste the Reviewer Checklist from here into a new comment on this PR and complete it. If you have the K2 extension, you can simply click: [this button]

@marufsharifi

marufsharifi commented Sep 21, 2026 •

Copy link
Copy Markdown
Contributor

Reviewer Checklist

  • I have verified the author checklist is complete (all boxes are checked off).
  • I verified the correct issue is linked in the ### Fixed Issues section above
  • I verified testing steps are clear and they cover the changes made in this PR
    • I verified the steps for local testing are in the Tests section
    • I verified the steps for Staging and/or Production testing are in the QA steps section
    • I verified the steps cover any possible failure scenarios (i.e. verify an input displays the correct error message if the entered data is not correct)
    • I turned off my network connection and tested it while offline to ensure it matches the expected behavior (i.e. verify the default avatar icon is displayed if app is offline)
  • I checked that screenshots or videos are included for tests on all platforms
  • I included screenshots or videos for tests on all platforms
  • I verified that the composer does not automatically focus or open the keyboard on mobile unless explicitly intended. This includes checking that returning the app from the background does not unexpectedly open the keyboard.
  • I verified tests pass on all platforms & I tested again on:
    • Android: HybridApp
    • Android: mWeb Chrome
    • iOS: HybridApp
    • iOS: mWeb Safari
    • MacOS: Chrome / Safari
  • If there are any errors in the console that are unrelated to this PR, I either fixed them (preferred) or linked to where I reported them in Slack
  • I verified proper code patterns were followed (see Reviewing the code)
    • I verified that comments were added to code that is not self explanatory
    • I verified that any new or modified comments were clear, correct English, and explained "why" the code was doing something instead of only explaining "what" the code was doing.
    • I verified any copy / text that was added to the app is grammatically correct in English. It adheres to proper capitalization guidelines (note: only the first word of header/labels should be capitalized), and is either coming verbatim from figma or has been approved by marketing (in order to get marketing approval, ask the Bug Zero team member to add the Waiting for copy label to the issue)
  • If a new code pattern is added I verified it was agreed to be used by multiple Expensify engineers
  • I verified that this PR follows the guidelines as stated in the Review Guidelines
  • I verified other components that can be impacted by these changes have been tested, and I retested again (i.e. if the PR modifies a shared library or component like Avatar, I verified the components using Avatar have been tested & I retested again)
  • If a new component is created I verified that:
    • A similar component doesn't exist in the codebase
    • All props are defined accurately
    • The component has a clear name that is non-ambiguous and the purpose of the component can be inferred from the name alone
    • The only data being stored in the state is data necessary for rendering and nothing else
    • The component has the minimum amount of code necessary for its purpose, and it is broken down into smaller components in order to separate concerns and functions
  • If a new CSS style is added I verified that:
    • A similar style doesn't already exist
    • The style can't be created with an existing StyleUtils function (i.e. StyleUtils.getBackgroundAndBorderStyle(theme.componentBG)
  • If the PR modifies code that runs when editing or sending messages, I tested and verified there is no unexpected behavior for all supported markdown - URLs, single line code, code blocks, quotes, headings, bold, strikethrough, and italic.
  • If the PR modifies a generic component, I tested and verified that those changes do not break usages of that component in the rest of the App (i.e. if a shared library or component like Avatar is modified, I verified that Avatar is working as expected in all cases)
  • If the PR modifies a component related to any of the existing Storybook stories, I tested and verified all stories for that component are still working as expected.
  • If the PR modifies a component or page that can be accessed by a direct deeplink, I verified that the code functions as expected when the deeplink is used - from a logged in and logged out account.
  • If the PR modifies the UI (e.g. new buttons, new UI components, changing the padding/spacing/sizing, moving components, etc) or modifies the form input styles:
    • I verified that all the inputs inside a form are aligned with each other.
    • I added Design label and/or tagged @Expensify/design so the design team can review the changes.
  • For any bug fix or new feature in this PR, I verified that sufficient unit tests are included to prevent regressions in this flow.
  • If the main branch was merged into this PR after a review, I tested again and verified the outcome was still expected according to the Test steps.
  • I have checked off every checkbox in the PR reviewer checklist, including those that don't apply to this PR.

Screenshots/Videos

Android: HybridApp
Android: mWeb Chrome
iOS: HybridApp
iOS: mWeb Safari
MacOS: Chrome / Safari

@marufsharifi marufsharifi left a comment

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.

LGTM

Comment thread config/rsbuild/rsbuild.common.ts Outdated
import {RsdoctorRspackPlugin} from '@rsdoctor/rspack-plugin';
import {rspack} from '@rspack/core';
import {execSync} from 'child_process';
import CompressionPlugin from 'compression-webpack-plugin';

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.

This plugin was deprecated in favour of a new one a few days ago. Should we change? https://github.com/webpack/compression-webpack-plugin

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.

Let's use https://github.com/ramon-villain/compression-rspack-plugin if we can? Just want to do what we can to keep build times from creeping back up.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Yes, the plugin was deprecated on September 19th. For now, it's just a README update (npm doesn't throw a warning and rspack still lists it as compatible). As for minimizer-webpack-plugin 5.11.0, it currently crashes under rspack. I’ve added a bit more context about this in the "Dependency note" section of the description.

Regarding compression-rspack-plugin: I'm a bit hesitant to introduce a dependency that only has 2 stars and hasn't seen any commits since March. And also it wouldn't improve our build times. It parallelizes across assets and compresses each asset with a single-threaded BrotliCompress. This is essentially doing the same thing as Node's zlib. Since our compression phase is bottlenecked by the largest single files, any per-asset parallelization will still hit the same ~12s floor. Their 3x speedup benchmark is based on 1,400 small assets, which doesn't reflect our use case.

If we'd prefer to avoid shipping a deprecated dependency, I think we have two good options:

  1. Keep it for now, open an issue/PR in rspack repo to fix the minimizer-webpack-plugin compatibility, and migrate once it's resolved.
  2. Or write our own custom compressor. It would only take about 40 lines of code (processAssets + zlib.brotliCompress) with zero external dependencies, meaning we’d have full control over any future fixes.
    I'd lean towards option 2 if we decide we definitely need to drop the current plugin. Which of these sounds better to you?

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.

Agree option 2 sounds better.

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.

2 sounds good to me

Comment thread config/rsbuild/rsbuild.common.ts Outdated
...(isDevelopment
? []
: [
new CompressionPlugin<BrotliOptions>({

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.

I believe you need the filename parameter here.

The default according to the docs is [path][base].gz but we want .br extensions now.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

When algorithm is brotliCompress, the plugin defaults filename to [path][base].br (source). But for visibility, I’ll add this as an explicit value, thanks! 🙂

@collectioneur
collectioneur force-pushed the collectioneur/compress-with-brotli branch from 514b7d9 to 0bab458 Compare September 23, 2026 12:02
@collectioneur

Copy link
Copy Markdown
Contributor Author

I created our custom plugin and compared the generated brotli files against the previous version byte by byte. Everything works exactly as before 👌

Comment thread config/rsbuild/rsbuild.common.ts Outdated
...(process.env.ANALYZE_BUNDLE === 'true' ? [new RsdoctorRspackPlugin()] : []),
// Writes a Brotli 11 twin (`foo.js` -> `foo.js.br`) beside every deployable text/bytecode asset, so the CDN
// can serve it instead of compressing with gzip on the fly: 25-30% fewer bytes over the wire.
...(isDevelopment ? [] : [new BrotliCompressionPlugin({test: /\.(?:js|css|html|svg|wasm|ttf)$/})]),

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.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Good catch, thanks! All fixed

roryabraham
roryabraham previously approved these changes Sep 23, 2026

@roryabraham roryabraham left a comment

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.

plugin code LGTM 👍🏼

LGTM except for @justinpersaud's comment

@marufsharifi marufsharifi left a comment

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.

Just one minor suggestion. LGTM

- name: Deploy to S3
run: |
aws s3 cp --recursive --acl public-read "$GITHUB_WORKSPACE"/dist ${{ env.S3_BUCKET }}/
aws s3 cp --recursive --acl public-read --exclude "*.br" "$GITHUB_WORKSPACE"/dist ${{ env.S3_BUCKET }}/

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.

NAB: /docs/* can silently return the NewDot app shell.

The dist here combines the web build (which has .br twins) with the Storybook build under ./dist/docs (which does not).

Because the CDN rewrite currently applies to /docs/*, a request like:

/docs/sb-manager/runtime.js → /docs/sb-manager/runtime.js.br

can miss the file and still return 200 HTML because of the SPA fallback. This can cause ERR_CONTENT_DECODING_FAILED or the browser to reject the script due to the text/html MIME type. A 404-based check won't catch it.

Could we please:

  1. Exclude /docs/* from the CloudFront rewrite and add a comment explaining why.
  2. Update the comment in BrotliCompressionPlugin.ts:17 — a missing twin does not necessarily mean a 404 because of the SPA fallback.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Exclude /docs/* from the CloudFront rewrite and add a comment explaining why.

cc: @justinpersaud

I think we need this since /docs is the Storybook build, which doesn't go through rspack, so none of its assets have .br twins, but they land in the same bucket behind the same distribution. WDYT?

justinpersaud
justinpersaud previously approved these changes Sep 24, 2026
@roryabraham
roryabraham merged commit 297b42c into Expensify:main Sep 29, 2026
34 of 35 checks passed
@github-actions

Copy link
Copy Markdown
Contributor

🚧 roryabraham has triggered a test Expensify/App build. You can view the workflow run here.

@OSBotify

Copy link
Copy Markdown
Contributor

✋ This PR was not deployed to staging yet because QA is ongoing. It will be automatically deployed to staging after the next production release.

@OSBotify

Copy link
Copy Markdown
Contributor

🚀 Deployed to staging by https://github.com/roryabraham in version: 9.4.96-0 🚀

platform result
🕸 web 🕸 success ✅
🤖 android 🤖 cancelled 🔪
🍎 iOS 🍎 cancelled 🔪

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

7 participants