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 bundle chunks #784

Merged
merged 6 commits into from
Jan 28, 2025
Merged

Optimize bundle chunks #784

merged 6 commits into from
Jan 28, 2025

Conversation

sergei-maertens
Copy link
Member

@sergei-maertens sergei-maertens commented Jan 24, 2025

Closes #76 - probably wise to wait until #782 is merged.

Some nice wins - our main chunk has now gone from 3.4MB -> 0.98MB. We have a 2MB chunk for everything Formio related, and then separate chunks for the appointments route (a modest 25kB) and the leaflet map stuff (360kB).

The generated assets:

image

Bundle analysis with npx vite-bundle-visualizer:

image

Tested with a production build in the backend with our e2e test suite and manual testing of course.

Copy link

codecov bot commented Jan 24, 2025

Bundle Report

Changes will decrease total bundle size by 12.8kB (-0.13%) ⬇️. This is within the configured threshold ✅

Detailed changes
Bundle name Size Change
@open-formulieren/sdk-OpenForms-umd 4.77MB 5.22kB (0.11%) ⬆️
@open-formulieren/sdk-esm 4.75MB 18.02kB (-0.38%) ⬇️

Copy link

codecov bot commented Jan 24, 2025

Codecov Report

Attention: Patch coverage is 86.06061% with 23 lines in your changes missing coverage. Please review.

Project coverage is 83.45%. Comparing base (385816d) to head (4bc9dc4).
Report is 12 commits behind head on main.

Files with missing lines Patch % Lines
src/components/Map/LeafletMap.jsx 82.22% 16 Missing ⚠️
src/components/FormStep/index.jsx 71.42% 4 Missing ⚠️
src/routes/appointments.jsx 89.28% 3 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main     #784      +/-   ##
==========================================
+ Coverage   83.41%   83.45%   +0.03%     
==========================================
  Files         240      242       +2     
  Lines        4752     4763      +11     
  Branches     1275     1265      -10     
==========================================
+ Hits         3964     3975      +11     
- Misses        754      755       +1     
+ Partials       34       33       -1     
Flag Coverage Δ
storybook 75.92% <83.95%> (+0.09%) ⬆️
vitest 63.00% <34.22%> (+0.24%) ⬆️

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

@sergei-maertens sergei-maertens force-pushed the chore/76-optimize-main-bundle branch 2 times, most recently from 73ca5b4 to 9a58271 Compare January 24, 2025 14:53
@sergei-maertens sergei-maertens force-pushed the chore/76-optimize-main-bundle branch from 9ed58b1 to 71a688b Compare January 24, 2025 16:07
@sergei-maertens sergei-maertens marked this pull request as ready for review January 24, 2025 16:58
@sergei-maertens sergei-maertens force-pushed the chore/76-optimize-main-bundle branch 2 times, most recently from a0f2792 to 0cd6fcf Compare January 24, 2025 17:08
src/sdk.jsx Show resolved Hide resolved
src/routes/app.jsx Show resolved Hide resolved
We can't properly extract lodash into a separate chunk because *many*
modules in formio import lodash as a whole rather than just the
modules they need, breaking tree shaking.

It also turns out (via 'npx vite-bundle-visualizer') that formio itself
is quite chonky and for the initial SDK load/routing we don't need
Formio yet, so we can defer loading that with a dynamic import. This
dynamic import ensures the formio-init code ends up in its own
chunk.
Couldn't find any references anymore that use(d) this function, and
since it imports Formio utils (which loads lodash too), it's better
to remove it to avoid bundling lodash in the main bundle.
Moving this away from the formio scope/namespace allows us to use it
in the rest of the non-formio related code without pulling in all of
lodash into the main bundle, allowing us to defer loading and have
a faster time-to-first-interact for end-users.

The utility itself is of questionnable importance, but that's a
refactor for another day.
More dead code that relied on Formio utils which caused our
bundles to be larger than necessary. The backend has been
updated a long time ago to provide the summary data to
display, rather than having to do this client-side.

The benefit is that no longer load anything from formio
outside of the formio-init chunk, which should result
in better perceived performance for end users.
Our React map component is now dynamically loaded, resulting in a separate chunk
of Javascript code that only gets loaded if/when map components are used in the
form, saving quite a bit of bytes for other forms.
@sergei-maertens sergei-maertens force-pushed the chore/76-optimize-main-bundle branch from 0cd6fcf to c9abd72 Compare January 28, 2025 09:01
Most forms won't need this code, so we can lazy load it instead.
@sergei-maertens sergei-maertens force-pushed the chore/76-optimize-main-bundle branch from c9abd72 to 4bc9dc4 Compare January 28, 2025 09:10
@robinmolen robinmolen merged commit f1af892 into main Jan 28, 2025
17 checks passed
@robinmolen robinmolen deleted the chore/76-optimize-main-bundle branch January 28, 2025 16:01
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.

Re-enable dynamic loading of i18n modules & put our bundle on a diet
2 participants