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

Optimize FidesJS bundle size by 40% by only loading TCF static strings when needed #4711

Merged
merged 14 commits into from
Mar 14, 2024

Conversation

NevilleS
Copy link
Contributor

@NevilleS NevilleS commented Mar 14, 2024

Closes PROD-1796

Description Of Changes

This optimizes the fides.js bundle size by splitting the static i18n message catalogs into two chunks:

  1. "Regular" messages: lib/i18n/locales/**/messages.json
  2. TCF-specific messages: lib/tcf/i18n/locales/**/messages-tcf.json

We then only load the TCF-specific messages during the initialization of the TcfOverlay, which is only called by the fides-tcf.js entrypoint. This allows rollup to correctly exclude all the TCF-specific JSON files from the default fides.js bundle and reduces the fides.js bundle size by ~22KB, an excellent 40% reduction!

Bundle Before Bundle Size (minified + gzipped) After Bundle Size (minified + gzipped) +/- Change
fides.js 53.5 KB 31.8 KB - 21.7 KB
fides-tcf.js 71.5 KB 72.8 KB ** + 1.3 KB**
src/fides.ts → dist/fides.js...
Browserslist: caniuse-lite is outdated. Please run:
  npx update-browserslist-db@latest
  Why you should do it regularly: https://github.com/browserslist/update-db#readme
✅ fides.js gzipped size passed maximum size checks (31.8 KB < 45 KB)
┌────────────────────────────────┐
│                                │
│   Destination: dist/fides.js   │
│   Bundle Size:  111.51 KB      │
│   Minified Size:  102.1 KB     │
│   Gzipped Size:  31.8 KB       │
│                                │
└────────────────────────────────┘
copied:
  dist/fides.js → ../privacy-center/public/lib/fides.js
  dist/fides.js → ../admin-ui/public/lib/fides.js
created dist/fides.js in 1s

Code Changes

  • Split messages.json -> messages.json and messages-tcf.json
  • Create new loadTcfMessagesFromFiles helper in lib/tcf/i18n to load TCF messages
  • Update unit tests 👍
  • Add rollup-plugin-visualizer to dev dependencies to visualize bundle:
    image
    (shows that the remaining locales data files are still about 15% of the overall bundle size, pre-minification/gzip)

Steps to Confirm

  • Run consent-i18n E2E tests and confirm that all existing translations work as expected!

Pre-Merge Checklist

Copy link

vercel bot commented Mar 14, 2024

The latest updates on your projects. Learn more about Vercel for Git ↗︎

1 Ignored Deployment
Name Status Preview Comments Updated (UTC)
fides-plus-nightly ⬜️ Ignored (Inspect) Visit Preview Mar 14, 2024 8:18pm

Copy link

cypress bot commented Mar 14, 2024

Passing run #6674 ↗︎

0 4 0 0 Flakiness 0
⚠️ You've recorded test results over your free plan limit.
Upgrade your plan to view test results.

Details:

Merge 074ebc9 into 7cae2de...
Project: fides Commit: 458e69de2d ℹ️
Status: Passed Duration: 00:36 💡
Started: Mar 14, 2024 7:16 PM Ended: Mar 14, 2024 7:17 PM

Review all test suite changes for PR #4711 ↗︎

Copy link
Contributor

@eastandwestwind eastandwestwind left a comment

Choose a reason for hiding this comment

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

Amazing! 💯

@@ -11,8 +11,8 @@ import commonjs from "@rollup/plugin-commonjs";

const NAME = "fides";
const IS_DEV = process.env.NODE_ENV === "development";
const GZIP_SIZE_ERROR_KB = 65; // fail build if bundle size exceeds this
const GZIP_SIZE_WARN_KB = 55; // log a warning if bundle size exceeds this
const GZIP_SIZE_ERROR_KB = 45; // fail build if bundle size exceeds this
Copy link
Contributor

Choose a reason for hiding this comment

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

🎉

* having to do any particularly difficult tree-shaking!
*/
import type { I18n, Locale } from "../../i18n";
import { STATIC_MESSAGES_TCF } from "./locales";
Copy link
Contributor

Choose a reason for hiding this comment

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

So basically, you've moved all the TCF-specific locales into the tcf-specific folder so that those don't get picked up and loaded into the message catalog by the non-TCF build. Then here, when we know we're working with the TCF-specific bundle, we load them in dynamically. Is that right?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yup, pretty much! The only difference is that it's not really "dynamically" - it's statically true that this entire file (tcf-i18n-utils) is only reachable from the fides-tcf.ts entrypoint, so that makes it really obvious to rollup that it's not needed by the regular fides.ts entrypoint.

Comment on lines +2 to +4
* TCF-specific i18n utility functions. These are isolated to a separate file so
* that Rollup can easily remove this code from the base fides.js bundle without
* having to do any particularly difficult tree-shaking!
Copy link
Contributor

Choose a reason for hiding this comment

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

OK so we can't share the non-tcf loadMessagesFromFiles method from i18n-utils.ts because of difficulties with Rollup, I remember running into this before, with trying to share methods between non-tcf and tcf

Copy link
Contributor Author

Choose a reason for hiding this comment

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

We could technically call any of those methods from this file without any issue; we just need to be careful not to go the other direction and have the tcf/i18n/** files get imported by anything in the base package 👍

@NevilleS NevilleS merged commit 7a386e9 into main Mar 14, 2024
12 checks passed
@NevilleS NevilleS deleted the PROD-1796-ns-optimize-fides-build-exclude-tcf-strings branch March 14, 2024 20:26
NevilleS added a commit that referenced this pull request Mar 14, 2024
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.

2 participants